2017-06-21 109 views
1

经过挖掘,我还没有发现任何与此问题有关的东西。C# - 应用程序配置文件 - 自定义设置

下面是我当前的代码:

配置文件

<?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); 
    } 
} 

我要完成,是不是有,在配置文件上,在 “服务” 元素 “添加” 是什么。所以,我想要下面的配置文件,没有“添加”。

任何帮助?先谢谢你。

<?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> 

回答

0

我想你的配置中有一个额外的图层,根据你想要的最终结果是什么。而不是找一个单独的服务节点,你应该抓住服务的集合:

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

然后,当你看,你可以在“服务”键,而不是默认的“添加”集合中的服务要素:

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

答案和评论here与您所做的相似,我认为。

+0

谢谢你的回答。在那篇文章中有5个答案和很多评论。我应该检查哪一个?你可以在这篇文章中编辑你的答案,并指出你提到的答案吗? –

+0

标记为答案及其附加评论的那个。评论讨论AddItemName参数。 – tattarrattat

+0

我最终删除了该组并将其作为一个部分启动。像魅力一样工作! –

相关问题