2021年4月25日星期日

.Net -

使用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>// ConfigurationElementpublic 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; } }}// ConfigurationElementCollectionpublic 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);  } }}// ConfigurationSectionpublic class CustomConfigurationSection : ConfigurationSection{ public CustomConfigurationSection() { } [ConfigurationProperty("customElementsCollection", IsDefaultCollection = false)] public CustomConfigurationElementCollection CustomElementsCollection {  get { return (CustomConfigurationElementCollection)base["customElementsCollection"]; } }}// usepublic 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}"); }}








原文转载:http://www.shaoqun.com/a/706737.html

跨境电商:https://www.ikjzd.com/

picitup:https://www.ikjzd.com/w/446

友家快递:https://www.ikjzd.com/w/1341


使用C#进行应用开发时,有时需要在程序配置文件中添加自定义的配置项,此时可以使用ConfigurationSection。本文参考链接:ConfigurationSection、ConfigurationElement、ConfigurationElementCollection、ConfigurationProperty及JamesJohnson的回复。文中有误的地方欢迎大家交流指正。Confi
腾邦:https://www.ikjzd.com/w/1382
刘军:https://www.ikjzd.com/w/1835
万色:https://www.ikjzd.com/w/2382
案例分析:外贸人不得不记住的惨痛教训!:https://www.ikjzd.com/home/101083
跨境电商Shopee也能做无货源店群?这个模式究竟如何操作?:https://www.ikjzd.com/home/108993
该国TS部门严查!不合规货物面临退运、销毁!:https://www.ikjzd.com/home/97697

没有评论:

发表评论