zoukankan      html  css  js  c++  java
  • .net config文件 配置类的集合

    1,appconfig文件

    <configSections>
        <section  name="ToolConfig" type="DMTools.ToolConfigSection,DMTools"/>
      </configSections>
      <ToolConfig >
        <DmTools>
          <AddTool ImagePath="picMapGen.png" Name="代码生成By设计文档"  Link="http://dmsite.chinacloudsites.cn/root/tools/CodeGenBySql/CodeGenBySql.application" />
          <AddTool ImagePath="picCodeGenBySql.png" Name="代码生成BySQl"  Link="http://dmsite.chinacloudsites.cn/root/tools/CodeGenBySql/CodeGenBySql.application" />
           <AddTool ImagePath="picSyncDataBase.png" Name="数据结构迁移工具"  Link="http://dmsite.chinacloudsites.cn/root/Tools/SysDataBase/SyncDataBase.application" />
          <AddTool ImagePath="picMegreFile.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();
  • 相关阅读:
    TypeScript 2.0 正式发布
    亚太物联网市场前景巨大 2020年预计达到793亿美元
    亚太物联网市场前景巨大 2020年预计达到793亿美元
    亚太物联网市场前景巨大 2020年预计达到793亿美元
    亚太物联网市场前景巨大 2020年预计达到793亿美元
    QTP VBScript RegExp对象的运用
    QTP VBScript RegExp对象的运用
    QTP VBScript RegExp对象的运用
    base64 编码和解码
    nginx 4层负载多个端口
  • 原文地址:https://www.cnblogs.com/damsoft/p/6128125.html
Copyright © 2011-2022 走看看