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 登録します。 例については、「例」セクションに示されている構成ファイルの抜粋を参照してください。

Note

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

注意 (実装者)

プログラムまたは宣言型 (属性付き) コーディング モデルを使用して、カスタム構成セクションを作成できます。

  • プログラムモデル。 このモデルでは、セクション属性ごとにプロパティを作成して値を取得または設定し、基になる ConfigurationElement 基底クラスの内部プロパティ バッグに追加する必要があります。

  • 宣言型モデル。 属性付きモデルとも呼ばれるこの単純なモデルを使用すると、プロパティを使用して属性で修飾することで、セクション属性を定義できます。 これらの属性は、プロパティの種類とその既定値について ASP.NET 構成システムに指示します。 リフレクションによって取得されたこの情報を使用すると、ASP.NET 構成システムによってセクション プロパティ オブジェクトが作成され、必要な初期化が実行されます。

クラスを Configuration 使用すると、構成ファイルを編集するためのプログラムによるアクセスが許可されます。 これらのファイルには、次のように読み取りまたは書き込み用にアクセスできます。

  • 読み取り。 または GetSectionGroup(String) を使用GetSection(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

ContextInformation オブジェクトの ConfigurationElement オブジェクトを取得します。

(継承元 ConfigurationElement)
HasContext

CurrentConfiguration プロパティが null であるかどうかを示す値を取得します。

(継承元 ConfigurationElement)
Item[ConfigurationProperty]

この構成要素のプロパティまたは属性を取得または設定します。

(継承元 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)

逆シリカル化中に不明な属性が発生したかどうかを示す値を取得します。

(継承元 ConfigurationElement)
OnDeserializeUnrecognizedElement(String, XmlReader)

逆シリカル化中に不明な要素が発生したかどうかを示す値を取得します。

(継承元 ConfigurationElement)
OnRequiredPropertyNotFound(String)

必要なプロパティが見つからないと例外がスローされます。

(継承元 ConfigurationElement)
PostDeserialize()

逆シリアル化後に呼び出されます。

(継承元 ConfigurationElement)
PreSerialize(XmlWriter)

シリアル化前に呼び出されます。

(継承元 ConfigurationElement)
Reset(ConfigurationElement)

ConfigurationElement オブジェクトの内部状態 (ロックやプロパティ コレクションなど) をリセットします。

(継承元 ConfigurationElement)
ResetModified()

IsModified() メソッドの値が派生クラスに実装されたときに、false にリセットします。

SerializeElement(XmlWriter, Boolean)

派生クラスに実装されている場合、この構成要素の内容を構成ファイルに書き込みます。

(継承元 ConfigurationElement)
SerializeSection(ConfigurationElement, String, ConfigurationSaveMode)

ファイルに書き込む 1 つのセクションとして、ConfigurationSection オブジェクトのアンマージされたビューを含む XML 文字列を作成します。

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)

適用対象

こちらもご覧ください