ConfigurationProperty 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示配置元素的一个特性或子级。 此类不能被继承。
此 API 支持产品基础结构,不能在代码中直接使用。
public ref class ConfigurationProperty sealedpublic sealed class ConfigurationPropertytype ConfigurationProperty = classPublic NotInheritable Class ConfigurationProperty- 继承
- 
				ConfigurationProperty
示例
- 下面的代码示例演示如何在创建自定义节时使用 ConfigurationProperty 。
using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
namespace ConfigurationPropertyExample
{
    // Define a custom section.
    // Shows how to use the ConfigurationProperty
    // class when defining a custom section.
    public sealed class CustomSection : ConfigurationSection
    {
        // The collection (property bag) that contains 
        // the section properties.
        private static ConfigurationPropertyCollection _Properties;
        // The FileName property.
        private static ConfigurationProperty _FileName;
        // The Alias property.
        private static ConfigurationProperty _Alias;
        // The MaxUsers property.
        private static ConfigurationProperty _MaxUsers;
        // The MaxIdleTime property.
        private static ConfigurationProperty _MaxIdleTime;
        // CustomSection constructor.
        static CustomSection()
        {
            // Initialize the _FileName property
            _FileName =
                new ConfigurationProperty("fileName",
                typeof(string), "default.txt");
            // Initialize the _MaxUsers property
            _MaxUsers =
                new ConfigurationProperty("maxUsers",
                typeof(long), (long)1000,
                ConfigurationPropertyOptions.None);
            // Initialize the _MaxIdleTime property
            TimeSpan minTime = TimeSpan.FromSeconds(30);
            TimeSpan maxTime = TimeSpan.FromMinutes(5);
            ConfigurationValidatorBase _TimeSpanValidator =
                new TimeSpanValidator(minTime, maxTime, false);
            _MaxIdleTime =
                new ConfigurationProperty("maxIdleTime",
                typeof(TimeSpan), TimeSpan.FromMinutes(5),
                TypeDescriptor.GetConverter(typeof(TimeSpan)),
                _TimeSpanValidator,
                ConfigurationPropertyOptions.IsRequired,
                "[Description:This is the max idle time.]");
            // Initialize the _Alias property
            _Alias =
                new ConfigurationProperty("alias",
                typeof(string), "alias.txt");
            // Initialize the Property collection.
            _Properties = new ConfigurationPropertyCollection();
            _Properties.Add(_FileName);
            _Properties.Add(_Alias);
            _Properties.Add(_MaxUsers);
            _Properties.Add(_MaxIdleTime);
        }
        // Return the initialized property bag
        // for the configuration element.
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _Properties;
            }
        }
        // Clear the property.
        public void ClearCollection()
        {
            Properties.Clear();
        }
        // Remove an element from the property collection.
        public void RemoveCollectionElement(string elName)
        {
            Properties.Remove(elName);
        }
        // Get the property collection enumerator.
        public IEnumerator GetCollectionEnumerator()
        {
            return (Properties.GetEnumerator());
        }
        [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
            MinLength = 1, MaxLength = 60)]
        public string FileName
        {
            get
            {
                return (string)this["fileName"];
            }
            set
            {
                this["fileName"] = value;
            }
        }
        [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
            MinLength = 1, MaxLength = 60)]
        public string Alias
        {
            get
            {
                return (string)this["alias"];
            }
            set
            {
                this["alias"] = value;
            }
        }
        [LongValidator(MinValue = 1, MaxValue = 1000000,
            ExcludeRange = false)]
        public long MaxUsers
        {
            get
            {
                return (long)this["maxUsers"];
            }
            set
            {
                this["maxUsers"] = value;
            }
        }
        public TimeSpan MaxIdleTime
        {
            get
            {
                return (TimeSpan)this["maxIdleTime"];
            }
            set
            {
                this["maxIdleTime"] = value;
            }
        }
    }
}
Imports System.Configuration
Imports System.Collections
Imports System.ComponentModel
' Define a custom section.
' Shows how to use the ConfigurationProperty
' class when defining a custom section.
Public NotInheritable Class CustomSection
    Inherits ConfigurationSection
    ' The collection (property bag) that contains 
    ' the section properties.
    Private Shared _Properties As ConfigurationPropertyCollection
    ' The FileName property.
    Private Shared _FileName As ConfigurationProperty
    ' The Alias property.
    Private Shared _Alias As ConfigurationProperty
    ' The MasUsers property.
    Private Shared _MaxUsers As ConfigurationProperty
    ' The MaxIdleTime property.
    Private Shared _MaxIdleTime As ConfigurationProperty
    ' CustomSection constructor.
    Shared Sub New()
        ' Initialize the _FileName property
        _FileName = New ConfigurationProperty( _
            "fileName", GetType(String), "default.txt")
        ' Initialize the _MaxUsers property
        _MaxUsers = New ConfigurationProperty( _
            "maxUsers", GetType(Long), 1000L, _
            ConfigurationPropertyOptions.None)
        ' Initialize the _MaxIdleTime property
        Dim minTime As TimeSpan = TimeSpan.FromSeconds(30)
        Dim maxTime As TimeSpan = TimeSpan.FromMinutes(5)
        Dim _TimeSpanValidator = _
            New TimeSpanValidator(minTime, maxTime, False)
        _MaxIdleTime = New ConfigurationProperty( _
            "maxIdleTime", GetType(TimeSpan), _
            TimeSpan.FromMinutes(5), _
            TypeDescriptor.GetConverter(GetType(TimeSpan)), _
            _TimeSpanValidator, _
            ConfigurationPropertyOptions.IsRequired, _
            "[Description:This is the max idle time.]")
        ' Initialize the _Alias property
        _Alias = New ConfigurationProperty( _
            "alias", GetType(String), "alias.txt")
        ' Property collection initialization.
        ' The collection (property bag) that contains 
        ' the properties is declared as:
        ' ConfigurationPropertyCollection _Properties;
        _Properties = New ConfigurationPropertyCollection()
        _Properties.Add(_FileName)
        _Properties.Add(_Alias)
        _Properties.Add(_MaxUsers)
        _Properties.Add(_MaxIdleTime)
    End Sub
    ' Return the initialized property bag
    ' for the configuration element.
    Protected Overrides ReadOnly Property Properties() _
    As ConfigurationPropertyCollection
        Get
            Return _Properties
        End Get
    End Property
    <StringValidator(InvalidCharacters:= _
    " ~!@#$%^&*()[]{}/;'""|\", MinLength:=1, _
    MaxLength:=60)> _
    Public Property FileName() As String
        Get
            Return CStr(Me("fileName"))
        End Get
        Set(ByVal value As String)
            Me("fileName") = value
        End Set
    End Property
    <StringValidator(InvalidCharacters:= _
    " ~!@#$%^&*()[]{}/;'""|\", MinLength:=1, _
    MaxLength:=60)> _
    Public Property [Alias]() As String
        Get
            Return CStr(Me("alias"))
        End Get
        Set(ByVal value As String)
            Me("alias") = value
        End Set
    End Property
    <LongValidator(MinValue:=1, _
    MaxValue:=1000000, ExcludeRange:=False)> _
    Public Property MaxUsers() As Long
        Get
            Return Fix(Me("maxUsers"))
        End Get
        Set(ByVal value As Long)
            Me("maxUsers") = value
        End Set
    End Property
    Public Property MaxIdleTime() As TimeSpan
        Get
            Return CType(Me("maxIdleTime"), TimeSpan)
        End Get
        Set(ByVal value As TimeSpan)
            Me("maxIdleTime") = value
        End Set
    End Property
End Class
下面是上一示例中代码使用的配置文件的摘录。
<configuration>
  <configSections>
    <section name="CustomSection" type="ConfigurationPropertyExample.CustomSection, ConfigurationPropertyExample"
      allowDefinition="Everywhere" allowExeDefinition="MachineToApplication"
      restartOnExternalChanges="true" />
  </configSections>
  <CustomSection fileName="override.txt" alias="alias.txt"
    maxUsers="1000" maxIdleTime="00:05:00" />
</configuration>
以下示例演示如何在代码中创建上一节。
// Define a custom section programmatically.
static void CreateSection()
{
    try
    {
        CustomSection customSection;
        // Get the current configuration file.
        System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);
        // Create the section entry  
        // in the <configSections> and the 
        // related target section in <configuration>.
        // Call it "CustomSection2" since the file in this 
        // example already has "CustomSection".
        if (config.Sections["CustomSection"] == null)
        {
            customSection = new CustomSection();
            config.Sections.Add("CustomSection2", customSection);
            customSection.SectionInformation.ForceSave = true;
            config.Save(ConfigurationSaveMode.Full);
        }
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine(err.ToString());
    }
}
' Create a custom section.
Shared Sub CreateSection()
    Try
        Dim customSection As CustomSection
        ' Get the current configuration file.
        Dim config As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)
        ' Create the section entry  
        ' in the <configSections> and the 
        ' related target section in <configuration>.
        ' Since the config file already has "CustomSection",
        ' call this one "CustomSection2".
        If config.Sections("CustomSection") Is Nothing Then
            customSection = New CustomSection()
            config.Sections.Add("CustomSection", customSection)
            customSection.SectionInformation.ForceSave = True
            config.Save(ConfigurationSaveMode.Full)
        End If
    Catch err As ConfigurationErrorsException
        Console.WriteLine(err.ToString())
    End Try
End Sub
注解
对于简单的 ConfigurationElement,例如 CustomSection 下一个示例中所示的 , ConfigurationProperty 对象表示属性,如 fileName。
对于更复杂的配置元素(例如包含子节的节),authenticationConfigurationProperty对象可以表示ConfigurationElement对象和属性。
ConfigurationPropertyCollection类表示 对象的集合,ConfigurationProperty这些对象可以是配置元素的属性或ConfigurationElement对象。
ConfigurationProperty类表示单个配置设置。 此类允许获取或设置特定配置实体的名称、类型和默认值 (属性或元素) ,并指定属性是必需属性、是元素键还是表示默认元素集合。
继承者说明
每个对象创建ConfigurationElement表示元素属性或子元素集合的 对象的内部ConfigurationPropertyCollectionConfigurationProperty集合。
不可自定义的信息和功能由 ElementInformation 属性提供的 ElementInformation 对象包含。
可以使用编程或声明性 (特性化) 编码模型创建自定义配置元素。
- 编程模型。 此模型要求为每个元素属性创建一个属性,以获取和/或设置其值,并将其添加到基础 ConfigurationElement 基类的内部属性包。 
- 声明性模型。 此更简单的模型(也称为特性化模型)允许您使用属性定义元素属性,并使用属性对其进行修饰。 这些属性指示 ASP.NET 配置系统有关属性类型及其默认值的信息。 利用通过反射获取的此信息,ASP.NET 配置系统将为你创建元素属性对象并执行所需的初始化。 
构造函数
| ConfigurationProperty(String, Type) | 此 API 支持产品基础结构,不能在代码中直接使用。 初始化 ConfigurationProperty 类的新实例。 | 
| ConfigurationProperty(String, Type, Object) | 此 API 支持产品基础结构,不能在代码中直接使用。 初始化 ConfigurationProperty 类的新实例。 | 
| ConfigurationProperty(String, Type, Object, ConfigurationPropertyOptions) | 此 API 支持产品基础结构,不能在代码中直接使用。 初始化 ConfigurationProperty 类的新实例。 | 
| ConfigurationProperty(String, Type, Object, TypeConverter, ConfigurationValidatorBase, ConfigurationPropertyOptions) | 此 API 支持产品基础结构,不能在代码中直接使用。 初始化 ConfigurationProperty 类的新实例。 | 
| ConfigurationProperty(String, Type, Object, TypeConverter, ConfigurationValidatorBase, ConfigurationPropertyOptions, String) | 此 API 支持产品基础结构,不能在代码中直接使用。 初始化 ConfigurationProperty 类的新实例。 | 
属性
| Converter | 此 API 支持产品基础结构,不能在代码中直接使用。 获取 TypeConverter,该转换器用于将此 ConfigurationProperty 转换为便于写入配置文件的 XML 表示形式。 | 
| DefaultValue | 此 API 支持产品基础结构,不能在代码中直接使用。 获取此 ConfigurationProperty 属性的默认值。 | 
| Description | 此 API 支持产品基础结构,不能在代码中直接使用。 获取与 ConfigurationProperty 关联的说明。 | 
| IsAssemblyStringTransformationRequired | 此 API 支持产品基础结构,不能在代码中直接使用。 指示在针对早期版本的 .NET Framework 序列化配置属性时,是否需要转换该属性的程序集名称。 | 
| IsDefaultCollection | 此 API 支持产品基础结构,不能在代码中直接使用。 获取一个值,该值指示属性是否为默认的元素集合。 | 
| IsKey | 此 API 支持产品基础结构,不能在代码中直接使用。 获取一个值,该值指示此 ConfigurationProperty 是否为 ConfigurationElement 包含对象的键。 | 
| IsRequired | 此 API 支持产品基础结构,不能在代码中直接使用。 获取一个值,该值指示此 ConfigurationProperty 是否为必需的。 | 
| IsTypeStringTransformationRequired | 此 API 支持产品基础结构,不能在代码中直接使用。 指示在针对早期版本的 .NET Framework 序列化配置属性时,是否需要转换该属性的类型名称。 | 
| IsVersionCheckRequired | 此 API 支持产品基础结构,不能在代码中直接使用。 指示进行序列化时是否应查询配置属性的父配置节,以确定是否应将配置属性序列化到 XML 中。 | 
| Name | 此 API 支持产品基础结构,不能在代码中直接使用。 获取此 ConfigurationProperty 的名称。 | 
| Type | 此 API 支持产品基础结构,不能在代码中直接使用。 获取此 ConfigurationProperty 对象的类型。 | 
| Validator | 此 API 支持产品基础结构,不能在代码中直接使用。 获取 ConfigurationValidatorAttribute,它用于验证此 ConfigurationProperty 对象。 | 
方法
| Equals(Object) | 此 API 支持产品基础结构,不能在代码中直接使用。 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 此 API 支持产品基础结构,不能在代码中直接使用。 作为默认哈希函数。(继承自 Object) | 
| GetType() | 此 API 支持产品基础结构,不能在代码中直接使用。 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 此 API 支持产品基础结构,不能在代码中直接使用。 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 此 API 支持产品基础结构,不能在代码中直接使用。 返回表示当前对象的字符串。(继承自 Object) |