app.config 的自定义配置 - 部分的集合? [英] Custom Configuration for app.config - collections of sections?

查看:35
本文介绍了app.config 的自定义配置 - 部分的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的头在剧烈地跳动!我以前做过这件事,但没有这样深入"或复杂,我尝试了不同的方法来实现这一点,但都失败了.

My head is absolutely pounding badly! I have done this before but not as "in depth" or complex as this and I have tried different ways to make this happen but all has failed.

所以,这是我在 app.config 中想要的自定义 XML:

So, this is the custom XML I want in the app.config:

<Profiles> <!--Collection-->
<Profile Name="Live">
   <Components>
    <Component Name="Portal" Type="Web" />
    <Component Name="Comms" Type="Web" />
    <Component Name="Scheduler" Type="WindowsService" ServiceName="LiveScheduler" />
  </Components>
  <Databases>
    <Database Name="Main" Connection="Data Source=.\SQL2008" />
    <Database Name="Staging" Connection="Data Source=SomeSite.co.uk" />
  </Databases>
</Profile>
<Profile Name="Test">
   <Components>
    <Component Name="Portal" Type="Web" />
    <Component Name="Comms" Type="Web" />
    <Component Name="Scheduler" Type="WindowsService" ServiceName="TestScheduler" />
  </Components>
  <Databases>
    <Database Name="Main" Connection="Data Source=.\SQL2008" />
    <Database Name="Staging" Connection="Data Source=Internal" />
  </Databases>
</Profile>
</Profiles>

所以是 Profile 的集合,每个 Profile 都有一个子元素的集合(Components 是一个集合,然后 Component 是一个元素)

So a collection of Profile with each profile having a collection of sub elements (Components is a collection, then Component is an element)

但是,除了多个配置文件之外,我目前拥有所有这些.我确实看到了问题,但不确定如何修复"它.

However I currently have all that EXCEPT for the multiple profiles. I kind of do see the problem but not sure how to "fix" it.

代码:

public class Profile : ConfigurationSection
{
    [ConfigurationProperty("Name", IsRequired=true)]
    public string Name
    {
        get
        {
            return base["Name"] as string;
        }
            set
            {
                base["Name"] = value;
            }
        }

        [ConfigurationProperty("Components")]
        public ComponentCollection Components
        {
            get { return ((ComponentCollection)(base["Components"])); }
        }

        [ConfigurationProperty("Databases")]
        public DatabaseCollection Databases
        {
            get
            {
                return ((DatabaseCollection)(base["Databases"]));
            }
        }
    }

    [ConfigurationCollection(typeof(Component), AddItemName="Component")] 
    public class ComponentCollection : ConfigurationElementCollection
    {        
        protected override ConfigurationElement CreateNewElement()
        {
            return new Component();
        }

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

        public Component this[int idx]
        {
            get
            {
                return base.BaseGet(idx) as Component;
            }
            set
            {
                if (base.BaseGet(idx) != null)
                {
                    base.BaseRemoveAt(idx);
                }
                this.BaseAdd(idx, value);
            }
        }

        public Component this[string key]
        {
            get
            {
                return base.BaseGet(key) as Component;
            }
            set
            {
                if (base.BaseGet(key) != null)
                {
                    base.BaseRemove(key);
                }
                this.BaseAdd(this.Count, value);
            }
        }
    }

    public class Component : ConfigurationElement
    {
        [ConfigurationProperty("Type", IsRequired = true)]
        public string Type
        {
            get
            {
                return this["Type"] as string;
            }
            set
            {
                this["Type"] = value;
            }
        }

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

        [ConfigurationProperty("ServiceName", IsRequired = false)]
        public string ServiceName
        {
            get
            {
                return this["ServiceName"] as string;
            }
            set
            {
                this["ServiceName"] = value;
            }
        }
    }

    [ConfigurationCollection(typeof(Database), AddItemName = "Database")] 
    public class DatabaseCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new Database();
        }

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


        public Database this[int idx]
        {
            get
            {
                return base.BaseGet(idx) as Database;
            }
            set
            {
                if (base.BaseGet(idx) != null)
                {
                    base.BaseRemoveAt(idx);
                }
                this.BaseAdd(idx, value);
            }
        }

        public Database this[string key]
        {
            get
            {
                return base.BaseGet(key) as Database;
            }
            set
            {
                if (base.BaseGet(key) != null)
                {
                    base.BaseRemove(key);;
                }

                this.BaseAdd(this.Count, value);
            }
        }
    }

    public class Database : ConfigurationElement
    {
        [ConfigurationProperty("Name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get
            {
                return this["Name"] as string;
            }
            set
            {
                this["Name"] = value;
            }
        }

        [ConfigurationProperty("Connection", IsKey = false, IsRequired = true)]
        public string Connection
        {
            get
            {
                return this["Connection"] as string;
            }
            set
            {
                this["Connection"] = value;
            }
        }
    }

推荐答案

您需要将配置部分上移一级.

You need to move your configuration section one level higher.

public class Profiles : ConfigurationSection
{
    [ConfigurationProperty("Profile")]
    public ProfileCollection Profile
    {
        get
        {
            return this["profile"] as ProfileCollection;
        }
    }
}    

这是我创建的一个部分.您应该能够通过以下操作让您的工作正常工作:

Here's a section that I created. You should be able to get yours working by following this:

public class ImportConfiguration : ConfigurationSection
{
    [ConfigurationProperty("importMap")]
    public ImportMapElementCollection ImportMap
    {
        get
        {
            return this["importMap"] as ImportMapElementCollection;
        }
    }
}

public class ImportColumnMapElement : ConfigurationElement
{
    [ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
    public string LocalName
    {
        get
        {
            return this["localName"] as string;
        }
        set
        {
            this["localName"] = value;
        }
    }

    [ConfigurationProperty("sourceName", IsRequired = true)]
    public string SourceName
    {
        get
        {
            return this["sourceName"] as string;
        }
        set
        {
            this["sourceName"] = value;
        }
    }
}

public class ImportMapElementCollection : ConfigurationElementCollection
{
    public ImportColumnMapElement this[object key]
    {
        get
        {
            return base.BaseGet(key) as ImportColumnMapElement;
        }
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }

    protected override string ElementName
    {
        get
        {
            return "columnMap";
        }
    }

    protected override bool IsElementName(string elementName)
    {
        bool isName = false;
        if (!String.IsNullOrEmpty(elementName))
            isName = elementName.Equals("columnMap");
        return isName;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ImportColumnMapElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ImportColumnMapElement)element).LocalName;
    }
}

这篇关于app.config 的自定义配置 - 部分的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆