C# - 应用程序配置文件 - 自定义设置

时间:2017-06-21 14:25:57

标签: c# asp.net .net

在挖掘之后,我还没有找到与此问题相关的任何内容。

以下是我目前的代码:

配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="customSettings" type="CustomAssembly.Configuration.CustomSettingsGroup, CustomAssmbly, Version=1.0.0.0, Culture=neutral">
            <section name="services" type="CustomAssmbly.Configuration.ServiceSection, Agrione.Common45, Version=1.0.0.0, Culture=neutral" requirePermission="false" allowLocation="true" allowDefinition="Everywhere" />
        </sectionGroup>
    </configSections>

    <customSettings>
        <services>
            <service>
                <add name="AgrioneApi"
                         isDefault="true"
                         serviceType="1"
                         url="http://localhost:8094/" />
            </service>
        </services>
    </customSettings>

</configuration>

CustomSettingsGroup

public class CustomSettingsGroup : ConfigurationSectionGroup
{
    [ConfigurationProperty("services", IsDefaultCollection = true)]
    public ServiceSection ServicesSection => Sections["services"] as ServiceSection;
}

ServiceSection类

public class ServiceSection : ConfigurationSection
{
    [ConfigurationProperty("service", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(BaseConfigCollection<ServiceConfigElement>), AddItemName = "add")]
    public BaseConfigCollection<ServiceConfigElement> Services => (BaseConfigCollection<ServiceConfigElement>)base["service"];

    public ServiceConfigElement GetDefault()
    {
        var result = (from ServiceConfigElement e in Services
                         where e.IsDefault
                         select e).FirstOrDefault() ?? Services[0];

        return result;
    }
}

BaseConfigCollection类

public class BaseConfigCollection<TConfigElement> : ConfigurationElementCollection
    where TConfigElement : BaseConfigElement, new()
{
    public new TConfigElement this[string name] => (TConfigElement)BaseGet(name);

    public TConfigElement this[int index]
    {
        get => (TConfigElement)BaseGet(index);
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

    public void Add(TConfigElement element)
    {
        BaseAdd(element);
    }

    public void Clear()
    {
        BaseClear();
    }

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

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

    public void Remove(TConfigElement element)
    {
        BaseRemove(element);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }

    public void Remove(string name)
    {
        BaseRemove(name);
    }
}

我想要完成的是,在配置文件中没有&#34;添加&#34;在&#34;服务&#34;元件。所以,我想在下面的配置文件,没有&#34;添加&#34;。

有任何帮助吗?提前谢谢。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="customSettings" type="CustomAssembly.Configuration.CustomSettingsGroup, CustomAssmbly, Version=1.0.0.0, Culture=neutral">
            <section name="services" type="CustomAssmbly.Configuration.ServiceSection, Agrione.Common45, Version=1.0.0.0, Culture=neutral" requirePermission="false" allowLocation="true" allowDefinition="Everywhere" />
        </sectionGroup>
    </configSections>

    <customSettings>
        <services>
            <service name="AgrioneApi"
                         isDefault="true"
                         serviceType="1"
                         url="http://localhost:8094/" />
        </services>
    </customSettings>
</configuration>

1 个答案:

答案 0 :(得分:0)

我认为你的配置中有一个额外的图层,基于你想要的最终结果。您应该获取服务集合,而不是寻找单独的服务节点:

[ConfigurationProperty("services", IsDefaultCollection = false)]

然后,当您在集合中查找服务元素时,您可以键入“service”而不是默认的“add”:

   [ConfigurationCollection(typeof(BaseConfigCollection<ServiceConfigElement>), AddItemName = "service")]

我认为答案和评论here与您正在做的事情类似。