2017-06-21 106 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

我最終刪除了該組並將其作爲一個部分啓動。像魅力一樣工作! –

相關問題