ConfigurationSection クラス
アセンブリ: System.Configuration (system.configuration.dll 内)


ConfigurationSection を使用して、カスタムのセクション型を実装します。ConfigurationSection クラスを拡張して、カスタム ハンドリングを提供し、さらにカスタム構成セクションにプログラムからアクセスできるようにします。
セクションは、その処理型を configSections のエントリに登録します。
![]() |
---|
以前のバージョンの .NET Framework では、プログラムによって構成設定を変更するために構成セクション ハンドラが使用されていました。現在、すべての既定の構成セクションは、ConfigurationSection クラスを拡張するクラスによって表されています。 |
-
プログラム モデル。このモデルでは、セクション属性ごとにプロパティを作成し、その値の取得と設定の両方、またはそのいずれかを行い、その値を基になる ConfigurationElement 基本クラスの内部プロパティ バッグに追加する必要があります。
-
宣言モデル。属性付きモデルとも呼ばれる、このより単純なモデルを使用すると、セクション属性をプロパティを使用して定義し、属性で装飾できます。これらの属性は、ASP.NET 構成システムに対して、プロパティの型およびその既定値について指示します。リフレクションによって取得したこの情報を使用して、ASP.NET 構成システムはセクション プロパティ オブジェクトを作成し、必要な初期化を実行します。
![]() |
---|
パス パラメータを受け取る静的な GetSection メソッドを使用する場合、パス パラメータは、コードを実行しているアプリケーションを参照している必要があります。そうでない場合は、パラメータが無視され、現在実行中のアプリケーションの構成情報が返されます。 |

プログラムによってカスタム セクションを実装する方法を次のコード例に示します。
属性モデルを使用して実装されたカスタム セクションを実装および使用する方法を示す完全な例については、ConfigurationElement のトピックを参照してください。
' Define a custom section. ' The CustomSection type alows to define a custom section ' programmatically. NotInheritable Public Class CustomSection Inherits ConfigurationSection ' The collection (property bag) that conatains ' 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 MasUsers 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 'New ' 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 'ThrowIfReadOnly ' 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. ' Renemmber 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 'CustomSection
// Define a custom section. // The CustomSection type alows to define a custom section // programmatically. 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; } } }
<?xml version="1.0" encoding="utf-8"?>
<configSections>
<section name="CustomSection" type="Samples.AspNet, 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>

System.Configuration.ConfigurationElement
System.Configuration.ConfigurationSection
派生クラス


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ConfigurationSection コンストラクタ
アセンブリ: System.Configuration (system.configuration.dll 内)



ConfigurationSection を使用する方法の例を次に示します。
' 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>. 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 'CreateSection
// Create a custom section. 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>. if (config.Sections["CustomSection"] == null) { customSection = new CustomSection(); config.Sections.Add("CustomSection", customSection); customSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } catch (ConfigurationErrorsException err) { Console.WriteLine(err.ToString()); } }

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ConfigurationSection プロパティ

名前 | 説明 | |
---|---|---|
![]() | ElementInformation | ConfigurationElement オブジェクトのカスタマイズできない情報と機能を格納する ElementInformation オブジェクトを取得します。 ( ConfigurationElement から継承されます。) |
![]() | LockAllAttributesExcept | ロックされている属性のコレクションを取得します。 ( ConfigurationElement から継承されます。) |
![]() | LockAllElementsExcept | ロックされている要素のコレクションを取得します。 ( ConfigurationElement から継承されます。) |
![]() | LockAttributes | ロックされている属性のコレクションを取得します。 ( ConfigurationElement から継承されます。) |
![]() | LockElements | ロックされている要素のコレクションを取得します。 ( ConfigurationElement から継承されます。) |
![]() | LockItem | 要素がロックされているかどうかを示す値を取得または設定します。 ( ConfigurationElement から継承されます。) |
![]() | SectionInformation | ConfigurationSection オブジェクトのカスタマイズできない情報と機能を格納する SectionInformation オブジェクトを取得します。 |

名前 | 説明 | |
---|---|---|
![]() | ElementProperty | ConfigurationElement オブジェクト自体を表す ConfigurationElementProperty オブジェクトを取得します。 ( ConfigurationElement から継承されます。) |
![]() | EvaluationContext | ConfigurationElement オブジェクトの ContextInformation オブジェクトを取得します。 ( ConfigurationElement から継承されます。) |
![]() | Item | オーバーロードされます。 この ConfigurationElement オブジェクトのプロパティ、属性、または子要素を取得または設定します。 ( ConfigurationElement から継承されます。) |
![]() | Properties | プロパティのコレクションを取得します。 ( ConfigurationElement から継承されます。) |

関連項目
ConfigurationSection クラスSystem.Configuration 名前空間
Configuration クラス
SectionInformation
ConfigurationElement クラス
その他の技術情報
configuration 要素 (全般設定スキーマ)configSections 要素 (全般設定スキーマ)
ConfigurationSection メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 現在の ConfigurationElement インスタンスを、指定したオブジェクトと比較します。 ( ConfigurationElement から継承されます。) |
![]() | GetHashCode | 現在の ConfigurationElement インスタンスを表す一意の値を取得します。 ( ConfigurationElement から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | IsReadOnly | ConfigurationElement オブジェクトが読み取り専用かどうかを示す値を取得します。 ( ConfigurationElement から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | DeserializeElement | 構成ファイルから XML を読み取ります。 ( ConfigurationElement から継承されます。) |
![]() | DeserializeSection | 構成ファイルから XML を読み取ります。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | GetRuntimeObject | 派生クラスでオーバーライドされると、カスタム オブジェクトを返します。 |
![]() | Init | ConfigurationElement オブジェクトを初期状態に設定します。 ( ConfigurationElement から継承されます。) |
![]() | InitializeDefault | ConfigurationElement オブジェクトの既定の値セットを初期化するために使用します。 ( ConfigurationElement から継承されます。) |
![]() | IsModified | オーバーライドされます。 派生クラスに実装された場合、この構成要素が最後の保存または読み込み以降に変更されたかどうかを示します。 |
![]() | ListErrors | この ConfigurationElement オブジェクトおよびすべてのサブ要素の無効なプロパティのエラーを、渡されたリストに追加します。 ( ConfigurationElement から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
![]() | OnDeserializeUnrecognizedAttribute | 逆シリカル化中に不明な属性が発生したかどうかを示す値を取得します。 ( ConfigurationElement から継承されます。) |
![]() | OnDeserializeUnrecognizedElement | 逆シリカル化中に不明な要素が発生したかどうかを示す値を取得します。 ( ConfigurationElement から継承されます。) |
![]() | OnRequiredPropertyNotFound | 必須プロパティが見つからなかったかどうかを示す値を取得します。 ( ConfigurationElement から継承されます。) |
![]() | PostDeserialize | 逆シリアル化後に呼び出されます。 ( ConfigurationElement から継承されます。) |
![]() | PreSerialize | シリアル化前に呼び出されます。 ( ConfigurationElement から継承されます。) |
![]() | Reset | ConfigurationElement オブジェクトの内部状態 (ロックやプロパティ コレクションなど) をリセットします。 ( ConfigurationElement から継承されます。) |
![]() | ResetModified | オーバーライドされます。 派生クラスに実装された場合、IsModified メソッドの値を false にリセットします。 |
![]() | SerializeElement | 派生クラスに実装されている場合、この構成要素の内容を構成ファイルに書き込みます。 ( ConfigurationElement から継承されます。) |
![]() | SerializeSection | ファイルに書き込む 1 つのセクションとして、ConfigurationSection オブジェクトのアンマージされたビューを含む XML 文字列を作成します。 |
![]() | SerializeToXmlElement | 派生クラスに実装されている場合、この構成要素の外側のタグを構成ファイルに書き込みます。 ( ConfigurationElement から継承されます。) |
![]() | SetPropertyValue | プロパティを指定した値に設定します。 ( ConfigurationElement から継承されます。) |
![]() | SetReadOnly | ConfigurationElement オブジェクトおよびすべてのサブ要素に IsReadOnly プロパティを設定します。 ( ConfigurationElement から継承されます。) |
![]() | Unmerge | 保存しないすべての値を削除するには、ConfigurationElement オブジェクトを変更します。 ( ConfigurationElement から継承されます。) |

関連項目
ConfigurationSection クラスSystem.Configuration 名前空間
Configuration クラス
SectionInformation
ConfigurationElement クラス
その他の技術情報
configuration 要素 (全般設定スキーマ)configSections 要素 (全般設定スキーマ)
ConfigurationSection メンバ
ConfigurationSection データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | ElementInformation | ConfigurationElement オブジェクトのカスタマイズできない情報と機能を格納する ElementInformation オブジェクトを取得します。 (ConfigurationElement から継承されます。) |
![]() | LockAllAttributesExcept | ロックされている属性のコレクションを取得します。(ConfigurationElement から継承されます。) |
![]() | LockAllElementsExcept | ロックされている要素のコレクションを取得します。(ConfigurationElement から継承されます。) |
![]() | LockAttributes | ロックされている属性のコレクションを取得します。 (ConfigurationElement から継承されます。) |
![]() | LockElements | ロックされている要素のコレクションを取得します。(ConfigurationElement から継承されます。) |
![]() | LockItem | 要素がロックされているかどうかを示す値を取得または設定します。(ConfigurationElement から継承されます。) |
![]() | SectionInformation | ConfigurationSection オブジェクトのカスタマイズできない情報と機能を格納する SectionInformation オブジェクトを取得します。 |

名前 | 説明 | |
---|---|---|
![]() | ElementProperty | ConfigurationElement オブジェクト自体を表す ConfigurationElementProperty オブジェクトを取得します。(ConfigurationElement から継承されます。) |
![]() | EvaluationContext | ConfigurationElement オブジェクトの ContextInformation オブジェクトを取得します。(ConfigurationElement から継承されます。) |
![]() | Item | オーバーロードされます。 この ConfigurationElement オブジェクトのプロパティ、属性、または子要素を取得または設定します。(ConfigurationElement から継承されます。) |
![]() | Properties | プロパティのコレクションを取得します。(ConfigurationElement から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 現在の ConfigurationElement インスタンスを、指定したオブジェクトと比較します。 (ConfigurationElement から継承されます。) |
![]() | GetHashCode | 現在の ConfigurationElement インスタンスを表す一意の値を取得します。 (ConfigurationElement から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | IsReadOnly | ConfigurationElement オブジェクトが読み取り専用かどうかを示す値を取得します。 (ConfigurationElement から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | DeserializeElement | 構成ファイルから XML を読み取ります。 (ConfigurationElement から継承されます。) |
![]() | DeserializeSection | 構成ファイルから XML を読み取ります。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | GetRuntimeObject | 派生クラスでオーバーライドされると、カスタム オブジェクトを返します。 |
![]() | Init | ConfigurationElement オブジェクトを初期状態に設定します。 (ConfigurationElement から継承されます。) |
![]() | InitializeDefault | ConfigurationElement オブジェクトの既定の値セットを初期化するために使用します。 (ConfigurationElement から継承されます。) |
![]() | IsModified | オーバーライドされます。 派生クラスに実装された場合、この構成要素が最後の保存または読み込み以降に変更されたかどうかを示します。 |
![]() | ListErrors | この ConfigurationElement オブジェクトおよびすべてのサブ要素の無効なプロパティのエラーを、渡されたリストに追加します。 (ConfigurationElement から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
![]() | OnDeserializeUnrecognizedAttribute | 逆シリカル化中に不明な属性が発生したかどうかを示す値を取得します。 (ConfigurationElement から継承されます。) |
![]() | OnDeserializeUnrecognizedElement | 逆シリカル化中に不明な要素が発生したかどうかを示す値を取得します。 (ConfigurationElement から継承されます。) |
![]() | OnRequiredPropertyNotFound | 必須プロパティが見つからなかったかどうかを示す値を取得します。 (ConfigurationElement から継承されます。) |
![]() | PostDeserialize | 逆シリアル化後に呼び出されます。 (ConfigurationElement から継承されます。) |
![]() | PreSerialize | シリアル化前に呼び出されます。 (ConfigurationElement から継承されます。) |
![]() | Reset | ConfigurationElement オブジェクトの内部状態 (ロックやプロパティ コレクションなど) をリセットします。 (ConfigurationElement から継承されます。) |
![]() | ResetModified | オーバーライドされます。 派生クラスに実装された場合、IsModified メソッドの値を false にリセットします。 |
![]() | SerializeElement | 派生クラスに実装されている場合、この構成要素の内容を構成ファイルに書き込みます。 (ConfigurationElement から継承されます。) |
![]() | SerializeSection | ファイルに書き込む 1 つのセクションとして、ConfigurationSection オブジェクトのアンマージされたビューを含む XML 文字列を作成します。 |
![]() | SerializeToXmlElement | 派生クラスに実装されている場合、この構成要素の外側のタグを構成ファイルに書き込みます。 (ConfigurationElement から継承されます。) |
![]() | SetPropertyValue | プロパティを指定した値に設定します。 (ConfigurationElement から継承されます。) |
![]() | SetReadOnly | ConfigurationElement オブジェクトおよびすべてのサブ要素に IsReadOnly プロパティを設定します。 (ConfigurationElement から継承されます。) |
![]() | Unmerge | 保存しないすべての値を削除するには、ConfigurationElement オブジェクトを変更します。 (ConfigurationElement から継承されます。) |

関連項目
ConfigurationSection クラスSystem.Configuration 名前空間
Configuration クラス
SectionInformation
ConfigurationElement クラス
その他の技術情報
configuration 要素 (全般設定スキーマ)configSections 要素 (全般設定スキーマ)
- ConfigurationSectionのページへのリンク