ConfigurationProperty 類別

定義

代表屬性或組態項目的子系。 此類別無法獲得繼承。

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

public ref class ConfigurationProperty sealed
public sealed class ConfigurationProperty
type ConfigurationProperty = class
Public NotInheritable Class ConfigurationProperty
繼承
ConfigurationProperty

範例

  1. 下列程式代碼範例示範如何在您建立自訂區段時使用 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

例如,如果是更複雜的組態專案,例如包含子區段的區段, authentication物件 ConfigurationProperty 可以代表 ConfigurationElement 對象和屬性。

類別ConfigurationPropertyCollection代表可以是組態專案之屬性或ConfigurationElement物件的物件集合ConfigurationProperty

類別 ConfigurationProperty 代表個別的組態設定。 這個類別可讓您取得或設定特定組態實體的名稱、類型和預設值, (屬性或專案) ,並指定屬性是必要屬性、是元素索引鍵,或代表默認專案集合。

給繼承者的注意事項

每個 ConfigurationElement 物件都會建立代表項目屬性或子專案集合的物件內部 ConfigurationPropertyCollection 集合 ConfigurationProperty

不可自訂的資訊和功能是由 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)

適用於

另請參閱