利用属性(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
2、CustomSection.cs
3、Program.cs
在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
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="SingleTagSection" type="System.Configuration.SingleTagSectionHandler"/>
- <section name="DictionarySection" type="System.Configuration.DictionarySectionHandler"/>
- <section name="NameValueSection" type="System.Configuration.NameValueSectionHandler" />
- <section name="CustomSection" type="ConfigSections.CustomSection,ConfigSections" />
- </configSections>
- <SingleTagSection setting1="One world" setting2=" one dream" />
- <DictionarySection>
- <add key="China" value="51" />
- <add key="USA" value="36" />
- <add key="RUS" value="23" />
- </DictionarySection>
- <NameValueSection>
- <add key="China" value="100" />
- <add key="USA" value="110" />
- <add key="RUS" value="72" />
- </NameValueSection>
- <CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />
- </configuration>
2、CustomSection.cs
- // Define a custom section.
- // The CustomSection type alows to define a custom section
- // programmatically.
- using System.Configuration;
- using System;
- namespace ConfigSections
- {
- public sealed class CustomSection : ConfigurationSection
- {
- // The collection (property bag) that conatains
- // the section properties.
- private static ConfigurationPropertyCollection _Properties;
- // Internal flag to disable
- // property setting.
- private static bool _ReadOnly;
- // The FileName property.
- private static readonly ConfigurationProperty _FileName =
- new ConfigurationProperty("fileName",
- typeof(string), "default.txt",
- ConfigurationPropertyOptions.IsRequired);
- // The MasUsers property.
- private static readonly ConfigurationProperty _MaxUsers =
- new ConfigurationProperty("maxUsers",
- typeof(long), (long)1000,
- ConfigurationPropertyOptions.None);
- // The MaxIdleTime property.
- private static readonly ConfigurationProperty _MaxIdleTime =
- new ConfigurationProperty("maxIdleTime",
- typeof(TimeSpan), TimeSpan.FromMinutes(5),
- ConfigurationPropertyOptions.IsRequired);
- // CustomSection constructor.
- public CustomSection()
- {
- // Property initialization
- _Properties =
- new ConfigurationPropertyCollection();
- _Properties.Add(_FileName);
- _Properties.Add(_MaxUsers);
- _Properties.Add(_MaxIdleTime);
- }
- // This is a key customization.
- // It returns the initialized property bag.
- protected override ConfigurationPropertyCollection Properties
- {
- get
- {
- return _Properties;
- }
- }
- private new bool IsReadOnly
- {
- get
- {
- return _ReadOnly;
- }
- }
- // Use this to disable property setting.
- private void ThrowIfReadOnly(string propertyName)
- {
- if (IsReadOnly)
- throw new ConfigurationErrorsException(
- "The property " + propertyName + " is read only.");
- }
- // Customizes the use of CustomSection
- // by setting _ReadOnly to false.
- // Remember you must use it along with ThrowIfReadOnly.
- protected override object GetRuntimeObject()
- {
- // To enable property setting just assign true to
- // the following flag.
- _ReadOnly = true;
- return base.GetRuntimeObject();
- }
- [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
- MinLength = 1, MaxLength = 60)]
- public string FileName
- {
- get
- {
- return (string)this["fileName"];
- }
- set
- {
- // With this you disable the setting.
- // Renemmber that the _ReadOnly flag must
- // be set to true in the GetRuntimeObject.
- ThrowIfReadOnly("FileName");
- this["fileName"] = value;
- }
- }
- [LongValidator(MinValue = 1, MaxValue = 1000000,
- ExcludeRange = false)]
- public long MaxUsers
- {
- get
- {
- return (long)this["maxUsers"];
- }
- set
- {
- this["maxUsers"] = value;
- }
- }
- [TimeSpanValidator(MinValueString = "0:0:30",
- MaxValueString = "5:00:0",
- ExcludeRange = false)]
- public TimeSpan MaxIdleTime
- {
- get
- {
- return (TimeSpan)this["maxIdleTime"];
- }
- set
- {
- this["maxIdleTime"] = value;
- }
- }
- }
- }
3、Program.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Configuration;
- using System.Collections.Specialized;
- using System.Collections;
- namespace ConfigSections
- {
- class Program
- {
- static void Main(string[] args)
- {
- IDictionary dictionary = ConfigurationManager.GetSection("SingleTagSection") as IDictionary;
- foreach (DictionaryEntry item in dictionary)
- {
- Console.Write(item.Value);
- }
- Console.WriteLine();
- dictionary = ConfigurationManager.GetSection("DictionarySection") as IDictionary;
- foreach (DictionaryEntry item in dictionary)
- {
- Console.WriteLine("{0}\t{1}", item.Key, item.Value);
- }
- Console.WriteLine();
- NameValueCollection nameValueCollection = ConfigurationManager.GetSection("NameValueSection") as NameValueCollection;
- foreach (string key in nameValueCollection.AllKeys)
- {
- Console.WriteLine("{0}\t{1}", key, nameValueCollection[key]);
- }
- CustomSection section = ConfigurationManager.GetSection("CustomSection") as CustomSection;
- Console.WriteLine(section.FileName);
- Console.WriteLine(section.MaxUsers);
- Console.ReadLine();
- }
- }
- }
这里和大家分享和学习如何学IT!