利用属性(Attribute)扩展元数据,关于自定义配置节(转载)

介绍
    在NET中可以用自己的 XML 配置元素来扩展标准的 ASP.NET 配置设置集。若要完成该操作,必须一个实现 System.Configuration.ConfigurationSection 类的 .NET Framework 类。
    netframework 主要包括一下处理程序:
    SingleTagSectionHandler 配置节返回类型为 Systems.Collections.IDictionary
    DictionarySectionHandler 配置节返回类型为 Systems.Collections.IDictionary
    NameValueSectionHandler 配置节返回类型为 Systems.Collections.Specialized.NameValueCollection

创建自定义配置节

    1、在<configSections/>中声明配置节,
    2、在<Custom element for configuration section>处设置配置节的具体配置。
    3、访问自定义配置数据

实现 ConfigurationSection 类创建自定义配置文件处理类

    1、实现配置文件处理程序.
    2、创建配置文件
    3、访问配置信息。
    
示例代码
    创建控制台应用程序ConfigSections
    1、App.config
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.     <configSections>
  4.         <section name="SingleTagSection" type="System.Configuration.SingleTagSectionHandler"/>
  5.         <section name="DictionarySection" type="System.Configuration.DictionarySectionHandler"/>
  6.         <section name="NameValueSection" type="System.Configuration.NameValueSectionHandler" />
  7.         <section  name="CustomSection" type="ConfigSections.CustomSection,ConfigSections"   />
  8.        
  9.     </configSections>
  10.     <SingleTagSection setting1="One world" setting2=" one dream"  />
  11.     <DictionarySection>
  12.         <add key="China" value="51" />
  13.         <add key="USA" value="36" />
  14.         <add key="RUS" value="23" />
  15.     </DictionarySection>
  16.     <NameValueSection>
  17.         <add key="China" value="100" />
  18.         <add key="USA" value="110" />
  19.         <add key="RUS" value="72" />
  20.     </NameValueSection>
  21.     <CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />
  22. </configuration>

    2、CustomSection.cs
  1. // Define a custom section.
  2. // The CustomSection type alows to define a custom section 
  3. // programmatically.
  4. using System.Configuration;
  5. using System;
  6. namespace ConfigSections
  7. {
  8.     public sealed class CustomSection : ConfigurationSection
  9.     {
  10.         // The collection (property bag) that conatains 
  11.         // the section properties.
  12.         private static ConfigurationPropertyCollection _Properties;
  13.         // Internal flag to disable 
  14.         // property setting.
  15.         private static bool _ReadOnly;
  16.         // The FileName property.
  17.         private static readonly ConfigurationProperty _FileName =
  18.             new ConfigurationProperty("fileName",
  19.             typeof(string), "default.txt",
  20.             ConfigurationPropertyOptions.IsRequired);
  21.         // The MasUsers property.
  22.         private static readonly ConfigurationProperty _MaxUsers =
  23.             new ConfigurationProperty("maxUsers",
  24.             typeof(long), (long)1000,
  25.             ConfigurationPropertyOptions.None);
  26.         // The MaxIdleTime property.
  27.         private static readonly ConfigurationProperty _MaxIdleTime =
  28.             new ConfigurationProperty("maxIdleTime",
  29.             typeof(TimeSpan), TimeSpan.FromMinutes(5),
  30.             ConfigurationPropertyOptions.IsRequired);
  31.         // CustomSection constructor.
  32.         public CustomSection()
  33.         {
  34.             // Property initialization
  35.             _Properties =
  36.                 new ConfigurationPropertyCollection();
  37.             _Properties.Add(_FileName);
  38.             _Properties.Add(_MaxUsers);
  39.             _Properties.Add(_MaxIdleTime);
  40.         }
  41.         // This is a key customization. 
  42.         // It returns the initialized property bag.
  43.         protected override ConfigurationPropertyCollection Properties
  44.         {
  45.             get
  46.             {
  47.                 return _Properties;
  48.             }
  49.         }
  50.         private new bool IsReadOnly
  51.         {
  52.             get
  53.             {
  54.                 return _ReadOnly;
  55.             }
  56.         }
  57.         // Use this to disable property setting.
  58.         private void ThrowIfReadOnly(string propertyName)
  59.         {
  60.             if (IsReadOnly)
  61.                 throw new ConfigurationErrorsException(
  62.                     "The property " + propertyName + " is read only.");
  63.         }
  64.         // Customizes the use of CustomSection
  65.         // by setting _ReadOnly to false.
  66.         // Remember you must use it along with ThrowIfReadOnly.
  67.         protected override object GetRuntimeObject()
  68.         {
  69.             // To enable property setting just assign true to
  70.             // the following flag.
  71.             _ReadOnly = true;
  72.             return base.GetRuntimeObject();
  73.         }
  74.         [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
  75.             MinLength = 1, MaxLength = 60)]
  76.         public string FileName
  77.         {
  78.             get
  79.             {
  80.                 return (string)this["fileName"];
  81.             }
  82.             set
  83.             {
  84.                 // With this you disable the setting.
  85.                 // Renemmber that the _ReadOnly flag must
  86.                 // be set to true in the GetRuntimeObject.
  87.                 ThrowIfReadOnly("FileName");
  88.                 this["fileName"] = value;
  89.             }
  90.         }
  91.         [LongValidator(MinValue = 1, MaxValue = 1000000,
  92.             ExcludeRange = false)]
  93.         public long MaxUsers
  94.         {
  95.             get
  96.             {
  97.                 return (long)this["maxUsers"];
  98.             }
  99.             set
  100.             {
  101.                 this["maxUsers"] = value;
  102.             }
  103.         }
  104.         [TimeSpanValidator(MinValueString = "0:0:30",
  105.             MaxValueString = "5:00:0",
  106.             ExcludeRange = false)]
  107.         public TimeSpan MaxIdleTime
  108.         {
  109.             get
  110.             {
  111.                 return (TimeSpan)this["maxIdleTime"];
  112.             }
  113.             set
  114.             {
  115.                 this["maxIdleTime"] = value;
  116.             }
  117.         }
  118.     }
  119. }

    3、Program.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.Collections.Specialized;
  7. using System.Collections;
  8. namespace ConfigSections
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             IDictionary dictionary = ConfigurationManager.GetSection("SingleTagSection"as IDictionary;
  15.             foreach (DictionaryEntry item in dictionary)
  16.             {
  17.                 Console.Write(item.Value);
  18.             }
  19.             Console.WriteLine();
  20.             dictionary = ConfigurationManager.GetSection("DictionarySection"as IDictionary;
  21.             foreach (DictionaryEntry item in dictionary)
  22.             {
  23.                 Console.WriteLine("{0}\t{1}", item.Key, item.Value);
  24.             }
  25.             Console.WriteLine();
  26.             NameValueCollection nameValueCollection = ConfigurationManager.GetSection("NameValueSection"as NameValueCollection;
  27.             foreach (string key in nameValueCollection.AllKeys)
  28.             {
  29.                 Console.WriteLine("{0}\t{1}", key, nameValueCollection[key]);
  30.             }
  31.             CustomSection section = ConfigurationManager.GetSection("CustomSection"as CustomSection;
  32.             Console.WriteLine(section.FileName);
  33.             Console.WriteLine(section.MaxUsers);
  34.             Console.ReadLine();
  35.         }
  36.     }
  37. }
这里和大家分享和学习如何学IT!
原文地址:https://www.cnblogs.com/fuchifeng/p/1395371.html