ConfigurationSection 類別

定義

代表組態檔中的區段。

public ref class ConfigurationSection abstract : System::Configuration::ConfigurationElement
public abstract class ConfigurationSection : System.Configuration.ConfigurationElement
type ConfigurationSection = class
    inherit ConfigurationElement
Public MustInherit Class ConfigurationSection
Inherits ConfigurationElement
繼承
ConfigurationSection
衍生

範例

下列範例示範如何以程序設計方式實作自定義區段。

如需示範如何使用屬性化模型實作和使用自訂區段的完整範例,請參閱 ConfigurationElement

// Define a custom section.
// The CustomSection type allows to define a custom section 
// programmatically.
public sealed class CustomSection : 
    ConfigurationSection
{
    // The collection (property bag) that contains 
    // 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 MaxUsers 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.
            // Remember 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;
        }
    }
}
' Define a custom section.
' The CustomSection type allows to define a custom section 
' programmatically.

NotInheritable Public Class CustomSection
   Inherits ConfigurationSection
   ' The collection (property bag) that contains 
   ' the section properties.
   Private Shared _Properties As ConfigurationPropertyCollection
   
   ' Internal flag to disable 
   ' property setting.
   Private Shared _ReadOnly As Boolean
   
   ' The FileName property.
    Private Shared _FileName As New ConfigurationProperty( _
    "fileName", GetType(String), _
    "default.txt", _
    ConfigurationPropertyOptions.IsRequired)
   
   ' The MaxUsers property.
    Private Shared _MaxUsers As New ConfigurationProperty( _
    "maxUsers", GetType(Long), _
    CType(1000, Long), _
    ConfigurationPropertyOptions.None)
   
   ' The MaxIdleTime property.
    Private Shared _MaxIdleTime As New ConfigurationProperty( _
    "maxIdleTime", GetType(TimeSpan), _
    TimeSpan.FromMinutes(5), _
    ConfigurationPropertyOptions.IsRequired)
   
   
   ' CustomSection constructor.
   Public Sub New()
      ' Property initialization
        _Properties = _
        New ConfigurationPropertyCollection()
      
      _Properties.Add(_FileName)
      _Properties.Add(_MaxUsers)
      _Properties.Add(_MaxIdleTime)
   End Sub
   
   
   ' This is a key customization. 
   ' It returns the initialized property bag.
    Protected Overrides ReadOnly Property Properties() _
    As ConfigurationPropertyCollection
        Get
            Return _Properties
        End Get
    End Property
   
   
   
   Private Shadows ReadOnly Property IsReadOnly() As Boolean
      Get
         Return _ReadOnly
      End Get
   End Property
   
   
   ' Use this to disable property setting.
   Private Sub ThrowIfReadOnly(propertyName As String)
      If IsReadOnly Then
            Throw New ConfigurationErrorsException( _
            "The property " + propertyName + " is read only.")
      End If
   End Sub
   
   
   
   ' Customizes the use of CustomSection
    ' by setting _ReadOnly to false.
   ' Remember you must use it along with ThrowIfReadOnly.
   Protected Overrides Function GetRuntimeObject() As Object
      ' To enable property setting just assign true to
      ' the following flag.
      _ReadOnly = True
      Return MyBase.GetRuntimeObject()
   End Function 'GetRuntimeObject
   
   
    <StringValidator( _
    InvalidCharacters:=" ~!@#$%^&*()[]{}/;'""|\", _
    MinLength:=1, MaxLength:=60)> _
    Public Property FileName() As String
        Get
            Return CStr(Me("fileName"))
        End Get
        Set(ByVal value As String)
            ' With this you disable the setting.
            ' Remember that the _ReadOnly flag must
            ' be set to true in the GetRuntimeObject.
            ThrowIfReadOnly("FileName")
            Me("fileName") = 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
   
   
    <TimeSpanValidator( _
    MinValueString:="0:0:30", _
    MaxValueString:="5:00:0", ExcludeRange:=False)> _
    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

下列範例是組態檔的摘錄,因為它適用於上一個範例。

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <configSections>
     <section name="CustomSection" type="Samples.AspNet. CustomSection, CustomConfigurationSection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" />
   </configSections>

   <CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />

 </configuration>

備註

您可以使用 ConfigurationSection 類別來實作自訂區段類型。 ConfigurationSection擴充 類別,以提供自定義處理和自定義組態區段的程式設計存取。 如需如何使用自定義組態區段的詳細資訊,請參閱 如何:使用 ConfigurationSection 建立自定義組態區段

區段會向專案中的專案 configSections 註冊其處理類型。 如需範例,請參閱範例一節中顯示的組態檔摘錄。

注意

在舊版的 .NET Framework 中,組態區段處理程式是用來以程序設計方式變更組態設定。 現在,所有預設組態區段都會以擴充類別的 ConfigurationSection 類別表示。

給實施者的注意事項

您可以使用程式設計或宣告式 (屬性化) 程式代碼模型來建立自訂組態區段:

  • 程序設計模型。 此模型需要針對每個區段屬性建立屬性來取得或設定其值,並將它新增至基礎 ConfigurationElement 基類的內部屬性包。

  • 宣告式模型。 這個更簡單的模型也稱為屬性模型,可讓您使用 屬性來定義區段屬性,並以屬性裝飾它。 這些屬性會指示 ASP.NET 組態系統有關屬性類型和預設值。 透過反映取得這項資訊,ASP.NET 組態系統會建立區段屬性物件,並執行必要的初始化。

類別 Configuration 允許以程式設計方式存取編輯組態檔。 您可以存取這些檔案來讀取或寫入,如下所示:

  • 讀取。 GetSection(String)您可以使用 或 GetSectionGroup(String) 讀取組態資訊。 請注意,讀取的使用者或進程必須具有下列許可權:

    • 目前組態階層層級之組態檔的讀取許可權。

    • 所有父組態檔的讀取許可權。

    如果您的應用程式需要只讀存取自己的組態,建議您在 Web 應用程式的情況下使用多載方法,或在GetSection(String)用戶端應用程式的情況下使用 GetSection 方法。

    這些方法提供目前應用程式的快取組態值存取權,其效能優於 Configuration 類別。

注意:如果您使用採用path參數的靜態GetSection方法,path參數必須參考執行程式碼的應用程式;否則會忽略 參數,並傳回目前執行之應用程式的組態資訊。

  • 撰寫。 您可以使用其中 Save 一種方法來撰寫組態資訊。 請注意,寫入的使用者或進程必須具有下列許可權:

    • 目前組態階層層級之組態檔和目錄的寫入許可權。

    • 所有組態檔的讀取許可權。

建構函式

ConfigurationSection()

初始化 ConfigurationSection 類別的新執行個體。

屬性

CurrentConfiguration

取得最上層 Configuration 執行個體的參考,這個執行個體表示目前 ConfigurationElement 執行個體所屬的組態階層架構。

(繼承來源 ConfigurationElement)
ElementInformation

取得 ElementInformation 物件,其中包含 ConfigurationElement 物件之不可自訂的資訊和功能。

(繼承來源 ConfigurationElement)
ElementProperty

取得表示 ConfigurationElementProperty 物件本身的 ConfigurationElement 物件。

(繼承來源 ConfigurationElement)
EvaluationContext

取得 ConfigurationElement 物件的 ContextInformation 物件。

(繼承來源 ConfigurationElement)
HasContext

取得值,指出 CurrentConfiguration 屬性是否為 null

(繼承來源 ConfigurationElement)
Item[ConfigurationProperty]

取得或設定此組態項目的屬性 (Property) 或屬性 (Attribute)。

(繼承來源 ConfigurationElement)
Item[String]

取得或設定此一組態項目的屬性或子項目。

(繼承來源 ConfigurationElement)
LockAllAttributesExcept

取得已鎖定屬性的集合。

(繼承來源 ConfigurationElement)
LockAllElementsExcept

取得已鎖定項目的集合。

(繼承來源 ConfigurationElement)
LockAttributes

取得已鎖定屬性的集合。

(繼承來源 ConfigurationElement)
LockElements

取得已鎖定項目的集合。

(繼承來源 ConfigurationElement)
LockItem

取得或設定值,指出此項目是否已被鎖定。

(繼承來源 ConfigurationElement)
Properties

取得屬性的集合。

(繼承來源 ConfigurationElement)
SectionInformation

取得 SectionInformation 物件,該物件包含 ConfigurationSection 物件之不可自訂的資訊和功能。

方法

DeserializeElement(XmlReader, Boolean)

從組態檔讀取 XML。

(繼承來源 ConfigurationElement)
DeserializeSection(XmlReader)

從組態檔讀取 XML。

Equals(Object)

將目前的 ConfigurationElement 執行個體與指定的物件相比較。

(繼承來源 ConfigurationElement)
GetHashCode()

取得表示目前 ConfigurationElement 執行個體的唯一值。

(繼承來源 ConfigurationElement)
GetRuntimeObject()

在衍生類別中覆寫時,傳回自訂物件。

GetTransformedAssemblyString(String)

傳回指定之組件名稱的轉換版本。

(繼承來源 ConfigurationElement)
GetTransformedTypeString(String)

傳回指定之型別名稱的轉換版本。

(繼承來源 ConfigurationElement)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Init()

ConfigurationElement 物件設定為它的初始狀態。

(繼承來源 ConfigurationElement)
InitializeDefault()

用來初始化 ConfigurationElement 物件的預設值集。

(繼承來源 ConfigurationElement)
IsModified()

在衍生類別中進行實作時,指出這個組態項目自上次儲存或載入後是否已修改。

IsReadOnly()

取得值,這個值表示 ConfigurationElement 物件是否唯讀。

(繼承來源 ConfigurationElement)
ListErrors(IList)

將這個 ConfigurationElement 物件中和所有子項目中的無效屬性錯誤加入傳遞的清單。

(繼承來源 ConfigurationElement)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnDeserializeUnrecognizedAttribute(String, String)

取得值,指出在還原序列化程序中是否遇到未知的屬性 (Attribute)。

(繼承來源 ConfigurationElement)
OnDeserializeUnrecognizedElement(String, XmlReader)

取得值,指出在還原序列化程序中是否遇到未知的項目。

(繼承來源 ConfigurationElement)
OnRequiredPropertyNotFound(String)

在找不到必要的屬性時擲回例外狀況 (Exception)。

(繼承來源 ConfigurationElement)
PostDeserialize()

還原序列化之後呼叫。

(繼承來源 ConfigurationElement)
PreSerialize(XmlWriter)

序列化之前呼叫。

(繼承來源 ConfigurationElement)
Reset(ConfigurationElement)

重設 ConfigurationElement 物件的內部狀態,包括鎖定和屬性的集合。

(繼承來源 ConfigurationElement)
ResetModified()

在衍生類別中實作時,將 IsModified() 方法的值重設為 false

SerializeElement(XmlWriter, Boolean)

在衍生類別中實作時,將此組態項目的內容寫入組態檔中。

(繼承來源 ConfigurationElement)
SerializeSection(ConfigurationElement, String, ConfigurationSaveMode)

建立 XML 字串,在該字串的單一區段中,包含了要寫入檔案之 ConfigurationSection 物件的取消合併檢視。

SerializeToXmlElement(XmlWriter, String)

在衍生類別中實作時,將此組態項目的外部標記寫入組態檔中。

(繼承來源 ConfigurationElement)
SetPropertyValue(ConfigurationProperty, Object, Boolean)

將屬性設定為指定的值。

(繼承來源 ConfigurationElement)
SetReadOnly()

設定 IsReadOnly() 物件和所有子項目的 ConfigurationElement 屬性。

(繼承來源 ConfigurationElement)
ShouldSerializeElementInTargetVersion(ConfigurationElement, String, FrameworkName)

指出當組態物件階層針對指定的目標版本串行化 .NET Framework 時,是否應該串行化指定的專案。

ShouldSerializePropertyInTargetVersion(ConfigurationProperty, String, FrameworkName, ConfigurationElement)

指出當組態物件階層針對指定的目標版本串行化 .NET Framework 時,是否應該串行化指定的屬性。

ShouldSerializeSectionInTargetVersion(FrameworkName)

指出當組態物件階層針對指定的 .NET Framework 目標版本串行化時,是否應該串行化目前的ConfigurationSection實例。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
Unmerge(ConfigurationElement, ConfigurationElement, ConfigurationSaveMode)

修改 ConfigurationElement 物件,以移除不應該儲存的所有值。

(繼承來源 ConfigurationElement)

適用於

另請參閱