使用C#進行應用開發時,有時需要在程式組態檔中添加自定義的配置項,此時可以使用ConfigurationSection,
本文參考鏈接:ConfigurationSection、ConfigurationElement、ConfigurationElementCollection、ConfigurationProperty及James Johnson的回復,文中有誤的地方歡迎大家交流指正,
ConfigurationProperty表示配置元素的一個特性或子級,可以設定其名稱、型別和默認值 ,并可以指定該屬性是否是必需的,是否為Key等,
ConfigurationElement表示組態檔中的配置元素,每一條配置項對應一條ConfigurationElement,ConfigurationElement是抽象類,通過繼承該類可以添加自定義配置屬性,
ConfigurationElementCollection表示組態檔中元素的集合,包含一組配置項,ConfigurationElementCollection繼承ConfigurationElement,并實作ICollection介面,
ConfigurationSection表示組態檔中的節,通過繼承該類可以方便的擴展自定義的節點資訊,通過ConfigurationManager.GetSection()方法可以獲取到自定義的Section,
示例
// config組態檔
<configSections>
<section name="customSection" type="ConsoleApplication1.CustomConfigurationSection,ConsoleApplication1"/>
</configSections>
<customSection>
<customElementsCollection>
<customElement name="name1" content="content1"/>
<customElement name="name2" content="content2"/>
<customElement name="name3" content="content3"/>
</customElementsCollection>
</customSection>
// ConfigurationElement
public class CustomConfigurationElement : ConfigurationElement
{
public CustomConfigurationElement(string name, string content)
{
this.Name = name;
this.Content = content;
}
public CustomConfigurationElement()
{
}
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("content", IsRequired = true)]
public string Content
{
get { return (string)this["content"]; }
set { this["content"] = value; }
}
}
// ConfigurationElementCollection
public class CustomConfigurationElementCollection : ConfigurationElementCollection
{
public CustomConfigurationElementCollection()
{
}
protected override ConfigurationElement CreateNewElement()
{
return new CustomConfigurationElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CustomConfigurationElement)element).Name;
}
protected override bool IsElementName(string elementName)
{
bool isName = false;
if(!string.IsNullOrEmpty(elementName))
isName = elementName.Equals("customElement");
return isName;
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "customElement"; }
}
public CustomConfigurationElement this[object key]
{
get { return (CustomConfigurationElement)BaseGet(key); }
}
public CustomConfigurationElement this[int index]
{
get { return (CustomConfigurationElement)BaseGet(index); }
set
{
if(BaseGet(index) != null)
BaseRemoveAt(index);
BaseAdd(index, value);
}
}
}
// ConfigurationSection
public class CustomConfigurationSection : ConfigurationSection
{
public CustomConfigurationSection()
{
}
[ConfigurationProperty("customElementsCollection", IsDefaultCollection = false)]
public CustomConfigurationElementCollection CustomElementsCollection
{
get { return (CustomConfigurationElementCollection)base["customElementsCollection"]; }
}
}
// use
public static void ReadConfigurationSection(string sectionName)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) as Configuration;
var customSection = config.GetSection(sectionName) as CustomConfigurationSection;
if(customSection == null)
{
Console.WriteLine($"read failed: {sectionName} is null");
return;
}
Console.WriteLine("custom configuration section info:");
for (int i = 0; i < customSection.CustomElementsCollection.Count; ++i)
{
Console.WriteLine($"\tName:{customSection.CustomElementsCollection[i].Name} Content:{customSection.CustomElementsCollection[i].Content}");
}
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine($"read configuration error: {err.Message}");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/280144.html
標籤:WPF
上一篇:ASP.NET CORE使用WebUploader對大檔案分片上傳,并通過ASP.NET CORE SignalR實時反饋后臺處理進度給前端展示
