.net config文件 配置类的集合

1,appconfig文件

<configSections>
    <section  name="ToolConfig" type="DMTools.ToolConfigSection,DMTools"/>
  </configSections>
  <ToolConfig >
    <DmTools>
      <AddTool ImagePath="pic\MapGen.png" Name="代码生成By设计文档"  Link="http://dmsite.chinacloudsites.cn/root/tools/CodeGenBySql/CodeGenBySql.application" />
      <AddTool ImagePath="pic\CodeGenBySql.png" Name="代码生成BySQl"  Link="http://dmsite.chinacloudsites.cn/root/tools/CodeGenBySql/CodeGenBySql.application" />
       <AddTool ImagePath="pic\SyncDataBase.png" Name="数据结构迁移工具"  Link="http://dmsite.chinacloudsites.cn/root/Tools/SysDataBase/SyncDataBase.application" />
      <AddTool ImagePath="pic\MegreFile.png" Name="SQL自动合并执行工具"  Link="http://dmsite.chinacloudsites.cn/root/Tools/MegreFile/MegreFile.application" />
    </DmTools>
  </ToolConfig>

2,配置类

namespace DMTools
{
   

    public class ToolConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("DmTools")]
      
        [ConfigurationCollection(typeof(DmToolConfigurationElementCollection), AddItemName = "AddTool", ClearItemsName = "ClearTool", RemoveItemName = "RemoveTool")]
        public DmToolConfigurationElementCollection DmToolElements
        {
            get { return (DmToolConfigurationElementCollection)this["DmTools"]; }
            set { this["DmTools"] = value; }
        }
    }


    public class DmToolConfigurationElementCollection : ConfigurationElementCollection
    {
        // 基本上,所有的方法都只要简单地调用基类的实现就可以了。

        public DmToolConfigurationElementCollection() : base(StringComparer.OrdinalIgnoreCase)    // 忽略大小写
        {
        }

        // 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
        new public DmToolConfigurationElement this[string name]
        {
            get
            {
                return (DmToolConfigurationElement)base.BaseGet(name);
            }
        }

        // 下面二个方法中抽象类中必须要实现的。
        protected override ConfigurationElement CreateNewElement()
        {
            return new DmToolConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DmToolConfigurationElement)element).Name;
        }

        // 说明:如果不需要在代码中修改集合,可以不实现Add, Clear, Remove
        public void Add(DmToolConfigurationElement setting)
        {
            this.BaseAdd(setting);
        }
        public void Clear()
        {
            base.BaseClear();
        }
        public void Remove(string name)
        {
            base.BaseRemove(name);
        }
    }


    public class DmToolConfigurationElement : ConfigurationElement    // 集合中的每个元素
    {
        [ConfigurationProperty("ImagePath", IsRequired = true)]
        public string ImagePath
        {
            get { return this["ImagePath"].ToString(); }
            set { this["ImagePath"] = value; }
        }

        [ConfigurationProperty("Name", IsRequired = true)]
        public string Name
        {
            get { return this["Name"].ToString(); }
            set { this["Name"] = value; }
        }
        [ConfigurationProperty("Link", IsRequired = true)]
        public string Link
        {
            get { return this["Link"].ToString(); }
            set { this["Link"] = value; }
        }
    }
}

3,代码使用

 

   ToolConfigSection toolSection= (ToolConfigSection)ConfigurationManager.GetSection("ToolConfig");
            lstTile = (from toolElement in toolSection.DmToolElements.Cast<DmToolConfigurationElement>()
                       select new Tile { Name = toolElement.Name, Link = toolElement.Link, ImagePath = toolElement.ImagePath }
                      ).ToList();

 

posted @ 2016-12-03 10:33  晓明的哥哥  阅读(472)  评论(0)    收藏  举报