http://msdn.microsoft.com/zh-cn/library/kyx77cz3(v=VS.80).aspx
示例
下面的代码示例将演示如何实现自定义的 ConfigurationElement。
此元素由自定义节用于定义自定义节或自定义元素集合。
using System; using System.Configuration; using System.Collections; namespace Samples.AspNet { // Define the UrlConfigElement. public class UrlConfigElement : ConfigurationElement { // Test flag. private static bool _displayIt = false; public UrlConfigElement(String newName, String newUrl, int newPort) { Name = newName; Url = newUrl; Port = newPort; } public UrlConfigElement() { } public UrlConfigElement(string elementName) { Name = elementName; } [ConfigurationProperty("name", DefaultValue = "Microsoft", IsRequired = true, IsKey = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("url", DefaultValue = "http://www.microsoft.com", IsRequired = true)] [RegexStringValidator(@"\w+:\/\/[\w.]+\S*")] public string Url { get { return (string)this["url"]; } set { this["url"] = value; } } [ConfigurationProperty("port", DefaultValue = (int)0, IsRequired = false)] [IntegerValidator(MinValue = 0, MaxValue = 8080, ExcludeRange = false)] public int Port { get { return (int)this["port"]; } set { this["port"] = value; } } protected override void DeserializeElement( System.Xml.XmlReader reader, bool serializeCollectionKey) { base.DeserializeElement(reader, serializeCollectionKey); // Enter your custom processing code here. if (_displayIt) { Console.WriteLine( "UrlConfigElement.DeserializeElement({0}, {1}) called", (reader == null) ? "null" : reader.ToString(), serializeCollectionKey.ToString()); } } protected override bool SerializeElement( System.Xml.XmlWriter writer, bool serializeCollectionKey) { bool ret = base.SerializeElement(writer, serializeCollectionKey); // Enter your custom processing code here. if (_displayIt) { Console.WriteLine( "UrlConfigElement.SerializeElement({0}, {1}) called = {2}", (writer == null) ? "null" : writer.ToString(), serializeCollectionKey.ToString(), ret.ToString()); } return ret; } protected override bool IsModified() { bool ret = base.IsModified(); // Enter your custom processing code here. Console.WriteLine("UrlConfigElement.IsModified() called."); return ret; } } }
以下是上一示例所使用配置的摘录。
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="MyUrls" type="Samples.AspNet.Configuration.UrlsSection, ConfigurationElement, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" /> </configSections> <MyUrls lockAllElementsExcept="urls"> <simple name="Microsoft" url="http://www.microsoft.com" port="0" /> <urls> <clear /> <add name="Microsoft" url="http://www.microsoft.com" port="0" lockAllAttributesExcept="port" /> <add name="Contoso" url="http://www.contoso.com/" port="8080" lockAllAttributesExcept="port" lockItem="true" /> </urls> </MyUrls> </configuration>
下面的代码示例演示如何实现自定义 ConfigurationElementCollection,它包含之前定义的元素。
using System; using System.Configuration; using System.Collections; namespace Samples.AspNet { // Define the UrlsCollection that contains // UrlsConfigElement elements. public class UrlsCollection : ConfigurationElementCollection { public UrlsCollection() { UrlConfigElement url = (UrlConfigElement)CreateNewElement(); // Add the element to the collection. Add(url); } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.AddRemoveClearMap; } } protected override ConfigurationElement CreateNewElement() { return new UrlConfigElement(); } protected override ConfigurationElement CreateNewElement( string elementName) { return new UrlConfigElement(elementName); } protected override Object GetElementKey(ConfigurationElement element) { return ((UrlConfigElement)element).Name; } public new string AddElementName { get { return base.AddElementName; } set { base.AddElementName = value; } } public new string ClearElementName { get { return base.ClearElementName; } set { base.AddElementName = value; } } public new string RemoveElementName { get { return base.RemoveElementName; } } public new int Count { get { return base.Count; } } public UrlConfigElement this[int index] { get { return (UrlConfigElement)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } new public UrlConfigElement this[string Name] { get { return (UrlConfigElement)BaseGet(Name); } } public int IndexOf(UrlConfigElement url) { return BaseIndexOf(url); } public void Add(UrlConfigElement url) { BaseAdd(url); // Add custom code here. } protected override void BaseAdd(ConfigurationElement element) { BaseAdd(element, false); // Add custom code here. } public void Remove(UrlConfigElement url) { if (BaseIndexOf(url) >= 0) BaseRemove(url.Name); } public void RemoveAt(int index) { BaseRemoveAt(index); } public void Remove(string name) { BaseRemove(name); } public void Clear() { BaseClear(); // Add custom code here. } } }
下面的代码示例演示如何实现自定义 ConfigurationSection 节,该节使用之前定义的元素。
using System; using System.Configuration; using System.Collections; namespace Samples.AspNet { // Define a custom section containing // a simple element and a collection of // the same element. It uses two custom // types: UrlsCollection and // UrlsConfigElement. public class UrlsSection : ConfigurationSection { // Test flag. private static bool _displayIt = false; // Declare the custom element type. // This element will also be part of // the custom collection. UrlConfigElement url; public UrlsSection() { // Create the element. url = new UrlConfigElement(); } [ConfigurationProperty("name", DefaultValue = "MyFavorites", IsRequired = true, IsKey = false)] [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } // Declare a simple element of the type // UrlConfigElement. In the configuration // file it corresponds to <simple .... />. [ConfigurationProperty("simple")] public UrlConfigElement Simple { get { UrlConfigElement url = (UrlConfigElement)base["simple"]; return url; } } // Declare a collection element represented // in the configuration file by the sub-section // <urls> <add .../> </urls> // Note: the "IsDefaultCollection = false" // instructs the .NET Framework to build a nested // section like <urls> ...</urls>. [ConfigurationProperty("urls", IsDefaultCollection = false)] public UrlsCollection Urls { get { UrlsCollection urlsCollection = (UrlsCollection)base["urls"]; return urlsCollection; } } protected override void DeserializeSection( System.Xml.XmlReader reader) { base.DeserializeSection(reader); // Enter your custom processing code here. if (_displayIt) { Console.WriteLine( "UrlsSection.DeserializeSection({0}) called", (reader == null) ? "null" : reader.ToString()); } } protected override string SerializeSection( ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode) { string s = base.SerializeSection(parentElement, name, saveMode); // Enter your custom processing code here. if (_displayIt) { Console.WriteLine( "UrlsSection.SerializeSection({0}, {1}, {2}) called = {3}", parentElement.ToString(), name, saveMode.ToString(), s); } return s; } } }
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
请参见