ConfigurationSectionとは? わかりやすく解説

ConfigurationSection クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

構成ファイル内のセクション表します

名前空間: System.Configuration
アセンブリ: System.Configuration (system.configuration.dll 内)
構文構文

Public MustInherit Class
 ConfigurationSection
    Inherits ConfigurationElement
Dim instance As ConfigurationSection
public abstract class ConfigurationSection
 : ConfigurationElement
public ref class ConfigurationSection abstract
 : public ConfigurationElement
public abstract class ConfigurationSection
 extends ConfigurationElement
public abstract class ConfigurationSection
 extends ConfigurationElement
解説解説

ConfigurationSection使用してカスタムセクション型を実装ます。ConfigurationSection クラス拡張してカスタム ハンドリング提供し、さらにカスタム構成セクションプログラムかアクセスできるようにします。

セクションは、その処理型を configSections のエントリに登録します

次の例に示す構成ファイル抜粋参照してください

メモメモ

以前のバージョン.NET Framework では、プログラムによって構成設定変更するために構成セクション ハンドラ使用されいました。現在、すべての既定構成セクションは、ConfigurationSection クラス拡張するクラスによって表されています。

実装時の注意 プログラム コーディング モデルまたは宣言 (属性付き) コーディング モデル使用してカスタム構成セクション作成できます

Configuration は、構成ファイル編集プログラムから行うことができるようにするクラスです。次に示す方法で、これらのファイルへの読み取りアクセスまたは書き込みアクセスできます アプリケーションが独自の構成対す読み取り専用アクセスを行う必要がある場合は、GetSection のオーバーロード メソッド使用することをお勧めします (Web アプリケーション場合)。クライアント アプリケーション場合は、GetSection メソッド使用します。 これらのメソッドにより、現在のアプリケーションキャッシュされた構成値にアクセスできます。こちらの方が、Configuration クラスよりもパフォーマンス優れてます。
メモメモ

パス パラメータ受け取静的な 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"?>

<configuration>

<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.Object
   System.Configuration.ConfigurationElement
    System.Configuration.ConfigurationSection
       派生クラス
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

ConfigurationSection コンストラクタ

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

ConfigurationSection クラス新しインスタンス初期化します。

名前空間: System.Configuration
アセンブリ: System.Configuration (system.configuration.dll 内)
構文構文

Dim instance As New ConfigurationSection
protected ConfigurationSection ()
protected:
ConfigurationSection ()
protected ConfigurationSection ()
protected function ConfigurationSection ()
解説解説
使用例使用例

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());
    }

}

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

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 メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
  名前 説明
プロテクト メソッド 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 データ型公開されるメンバを以下の表に示します


プロテクト コンストラクタプロテクト コンストラクタ
  名前 説明
プロテクト メソッド 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 から継承されます。)
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
  名前 説明
プロテクト メソッド 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 要素 (全般設定スキーマ)



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ConfigurationSection」の関連用語

ConfigurationSectionのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ConfigurationSectionのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS