欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点

程序员文章站 2022-04-05 08:14:37
  除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效...

 

除了使用继承IConfigurationSectionHandler的方法定义处理自定义节点的类,还可以通过继承ConfigurationSection类实现同样效果。

 

首先说下.Net配置文件中一个潜规则:

 

在配置节点时,对于想要进行存储的参数数据,可以采用两种方式:一种是存储到节点的属性中,另一种是存储在节点的文本中。

因为一个节点可以有很多属性,但是只要一个innertext,而要在程序中将这两种形式区分开会带来复杂性。 为了避免这个问题,.net的配置文件只是用属性存储而不使用innertext.

 

接着,我们来写一个符合这个潜规则的自定义配置文件,方便测试:

 


    
      
      
      
      
    
  

 

接着,我们来写相应的处理类,这里我们由内向外来写:

 

首先是最内层的mailServer:

 

 

 /// 
    /// Class MailServerElement:用于映射mailServer节点,这里是实际存储数据的地方;
    /// 
    /// Editor:v-liuhch CreateTime:2015/6/27 21:51:57
    public sealed class MailServerElement : ConfigurationElement  //配置文件中的配置元素
    {

        /// 
        /// Gets or sets the client.
        /// 
        /// The client.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:05:40
        [ConfigurationProperty("client", IsKey = true, IsRequired = true)]  //client是必须的key属性,有点儿主键的意思,例如,如果定义多个client相同的节点,循环读取的话就只读取到最后一个值
        public string Client
        {
            get
            {
                return this["client"] as string;
            }
            set
            {
                this["client"] = value;
            }

        }
        /// 
        /// Gets or sets the address.
        /// 
        /// The address.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:05:38
        [ConfigurationProperty("address")]
        public string Address
        {
            get
            {
                return this["address"] as string;
            }
            set
            {
                this["address"] = value;
            }

        }
        /// 
        /// Gets or sets the name of the user.
        /// 
        /// The name of the user.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:05:35
        [ConfigurationProperty("userName")]
        public string UserName
        {

            get
            {
                return this["userName"] as string;
            }
            set
            {
                this["userName"] = value;
            }

        }
        /// 
        /// Gets or sets the password.
        /// 
        /// The password.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:05:33
        [ConfigurationProperty("password")]
        public string Password
        {

            get
            {
                return this["password"] as string;
            }
            set
            {
                this["password"] = value;
            }

        }



    }

 

 

 

接着是mailServers,它是一个mailServer的集合:

 

 

 /// 
    /// Class MailServerCollection:映射mailServers节点,为一个集合类,另外还包含了很多对节点的操作方法,大部分继承自ConfigurationElementCollection
    /// 
    /// Editor:v-liuhch CreateTime:2015/6/27 21:52:00
    public sealed class MailServerCollection : ConfigurationElementCollection
    {
        /// 
        /// 获取  的类型。
        /// 
        /// The type of the collection.
        /// 此集合的 。
        /// Editor:v-liuhch CreateTime:2015/6/27 22:05:08
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
         
        }


        /// 
        /// 当在派生的类中重写时,创建一个新的 。
        /// 
        /// 新的 。
        /// Editor:v-liuhch CreateTime:2015/6/27 22:05:03
        protected override ConfigurationElement CreateNewElement()
        {
            return new MailServerElement();
        }

        /// 
        /// 在派生类中重写时获取指定配置元素的元素键。
        /// 
        ///要为其返回键的 。
        /// 一个 ,用作指定  的键。
        /// Editor:v-liuhch CreateTime:2015/6/27 22:04:51
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as MailServerElement).Client;
        }

        /// 
        /// 获取在派生的类中重写时用于标识配置文件中此元素集合的名称。
        /// 
        /// The name of the element.
        /// 集合的名称;否则为空字符串。默认值为空字符串。
        /// Editor:v-liuhch CreateTime:2015/6/27 23:41:40
        protected override string ElementName
        {
            get
            {
                return "mailServer";
            }
        }


        /// 
        /// 获取集合中的元素数。
        /// 
        /// The count.
        /// 集合中的元素数。
        /// Editor:v-liuhch CreateTime:2015/6/27 22:08:24
        public new int Count
        {
            get { return base.Count; }
        }

        /// 
        /// 获取或设置此配置元素的属性、特性或子元素。
        /// 
        ///The index.
        /// MailServerElement.
        /// Editor:v-liuhch
        public MailServerElement this[int index]
        {

            get { return BaseGet(index) as MailServerElement; }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }

        }

        /// 
        /// 获取或设置此配置元素的属性、特性或子元素。
        /// 
        ///The name.
        /// MailServerElement.
        /// Editor:v-liuhch
        new public MailServerElement this[string Name]
        {
            get { return BaseGet(Name) as MailServerElement; }
        }

        /// 
        /// Indexes the of.
        /// 
        ///The element.
        /// System.Int32.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:24:16
        public int IndexOf(MailServerElement element)
        {

            return BaseIndexOf(element);
        }

        /// 
        /// Adds the specified element.
        /// 
        ///The element.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:26:06
        public void Add(MailServerElement element)
        {
            BaseAdd(element);
        }

        /// 
        /// Removes the specified element.
        /// 
        ///The element.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:27:01
        public void Remove(MailServerElement element)
        {
            if (BaseIndexOf(element) > 0)
            {
                BaseRemove(element.Client);
            }
        }

        /// 
        /// Removes at.
        /// 
        ///The index.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:33:29
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        /// 
        /// Removes the specified client.
        /// 
        ///The client.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:34:04
        public void Remove(string client)
        {
            BaseRemove(client);
        }

        /// 
        /// Clears this instance.
        /// 
        /// Editor:v-liuhch CreateTime:2015/6/27 22:34:29
        public void Clear()
        {
            BaseClear();
        }
    }

 

最后是最外层的group:

 

 

 /// 
    /// Class MailServerSection 为入口:
    /// 
    /// Editor:v-liuhch CreateTime:2015/6/27 21:41:02
    public class MailServerSection : ConfigurationSection   //继承配置文件中节
    {
        /// 
        /// Gets the provider.:映射mailServerGroup节点的provider
        /// 
        /// The provider.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:05:59
        [ConfigurationProperty("provider", IsKey = true)]
        public string provider { get { return this["provider"] as string; } }

        /// 
        /// Gets or sets the mail servers.:映射新添加的节点mailServers节点;这个节点下还包含了若干个mailServer节点,因此它是一个集合类
        /// 
        /// The mail servers.
        /// Editor:v-liuhch CreateTime:2015/6/27 22:05:56
        [ConfigurationProperty("mailServers", IsDefaultCollection = false)]
        public MailServerCollection MailServers
        {
            get
            {
                return this["mailServers"] as MailServerCollection;
            }
            set
            {
                this["mailServers"] = value;
            }

        }
    }

 

 

同样,关联处理类和节点:

 

 

 
 

 

 

之后做个测试:

 

 

class Program
    {
        static void Main(string[] args)
        {
            Test();

        }

        /// 
        /// Tests this instance.:读取节点值示例
        /// 
        /// Editor:v-liuhch CreateTime:2015/6/27 23:04:53
        private static void Test() {

            MailServerSection mailSection = (MailServerSection)ConfigurationManager.GetSection("mailServerGroup");
            Console.WriteLine("MailServerSection 的provider属性值:"+mailSection.provider);
            foreach (MailServerElement config in mailSection.MailServers)
            {
                Console.WriteLine("----------------------------------");
                Console.WriteLine("client值为:"+config.Client);
                Console.WriteLine("address值为:"+config.Address);
                Console.WriteLine("username值为:"+config.UserName);
                Console.WriteLine("password值为:"+config.Password);
                Console.WriteLine("----------------------------------");
            }

            Console.ReadKey();

        }

    }

 

 

 

 

本来还想传张结果图,但是网速慢,算啦,喜欢玩儿的童鞋自己run下结果。。。。。