ConfigurationSectionGroup クラス

定義

構成ファイル内の関連するセクションのグループを表します。

public ref class ConfigurationSectionGroup
public class ConfigurationSectionGroup
type ConfigurationSectionGroup = class
Public Class ConfigurationSectionGroup
継承
ConfigurationSectionGroup
派生

次の例は、 クラスを使用して構成設定を ConfigurationSectionGroup 取得する方法を示しています。 この例は、構成設定を読み取り、各構成セクション グループとそのセクションに関する情報をコンソールに書き込むコンソール アプリケーションです。

メソッドはMain、構成設定を オブジェクトに読み込みConfiguration、 オブジェクトからコレクションをConfiguration取得SectionGroupsし、 メソッドをShowSectionGroupCollectionInfo呼び出してセクション プロパティの値を表示します。

メソッドは ShowSectionGroupCollectionInfo セクション グループを反復処理し、それぞれに対して メソッドを ShowSectionGroupInfo 呼び出します。

メソッドは ShowSectionGroupInfo 、セクション グループの名前、いくつかのプロパティ値、およびセクションに含まれるセクションの名前を表示します。 セクション グループにセクション グループが含まれている場合、このメソッドは再帰的に を呼び出 ShowSectionGroupCollectionInfo して、それらのセクション グループを表示します。

フィールドを indentLevel 使用して、表示される行の左側にスペースを追加し、論理グループを表示します。 行の折り返しを避けるために、すべての行は 79 文字のテキストに制限されているため、論理グループを区別するのが難しくなります。

using System;
using System.Collections;
using System.Configuration;

namespace Samples.AspNet
{
    class UsingConfigurationSectionGroup
    {
        static int indentLevel = 0;

        static void Main(string[] args)
        {

            // Get the application configuration file.
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

            // Get the collection of the section groups.
            ConfigurationSectionGroupCollection sectionGroups =
                config.SectionGroups;

            // Display the section groups.
            ShowSectionGroupCollectionInfo(sectionGroups);
        }

        static void ShowSectionGroupCollectionInfo(
            ConfigurationSectionGroupCollection sectionGroups)
        {
            foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
            {
                ShowSectionGroupInfo(sectionGroup);
            }
        }

        static void ShowSectionGroupInfo(
            ConfigurationSectionGroup sectionGroup)
        {
            // Get the section group name.
            indent("Section Group Name: " + sectionGroup.Name);

            // Get the fully qualified group name.
            indent("Section Group Name: " + sectionGroup.SectionGroupName);

            indentLevel++;

            indent("Type: " + sectionGroup.Type);
            indent("Is Group Required?: " + 
                sectionGroup.IsDeclarationRequired);
            indent("Is Group Declared?: " + sectionGroup.IsDeclared);
            indent("Contained Sections:");

            indentLevel++;
            foreach (ConfigurationSection section 
                in sectionGroup.Sections)
            {
                indent("Section Name:" + section.SectionInformation.Name);
            }
            indentLevel--;

            // Display contained section groups if there are any.
            if (sectionGroup.SectionGroups.Count > 0)
            {
                indent("Contained Section Groups:");

                indentLevel++;
                ConfigurationSectionGroupCollection sectionGroups =
                    sectionGroup.SectionGroups;
                ShowSectionGroupCollectionInfo(sectionGroups);
            }

            Console.WriteLine("");
            indentLevel--;
        }

        static void indent(string text)
        {
            for (int i = 0; i < indentLevel; i++)
            {
                Console.Write("  ");
            }
            Console.WriteLine(text.Substring(0, Math.Min(79 - indentLevel * 2, text.Length)));
        }
    }
}
Imports System.Collections
Imports System.Configuration

Class UsingConfigurationSectionGroup
   Private Shared indentLevel As Integer = 0
    
    Public Shared Sub Main(ByVal args() As String)

        ' Get the application configuration file.
        Dim config As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

        ' Get the collection of the section groups.
        Dim sectionGroups As ConfigurationSectionGroupCollection = _
            config.SectionGroups

        ' Display the section groups.
        ShowSectionGroupCollectionInfo(sectionGroups)
    End Sub

    Shared Sub ShowSectionGroupCollectionInfo( _
        ByVal sectionGroups _
        As ConfigurationSectionGroupCollection)

        Dim group As ConfigurationSectionGroup
        For Each group In sectionGroups
            ShowSectionGroupInfo(group)
        Next group
    End Sub

    Shared Sub ShowSectionGroupInfo( _
    ByVal sectionGroup As ConfigurationSectionGroup)
        ' Get the section group name.
        indent("Section Group Name: " + sectionGroup.Name)

        ' Get the fully qualified section group name.
        indent("Section Group Name: " + sectionGroup.SectionGroupName)

        indentLevel += 1

        indent("Type: " + sectionGroup.Type)
        indent("Is Group Required?: " + _
           sectionGroup.IsDeclarationRequired.ToString())
        indent("Is Group Declared?: " + _
            sectionGroup.IsDeclared.ToString())
        indent("Contained Sections:")

        indentLevel += 1
        Dim section As ConfigurationSection
        For Each section In sectionGroup.Sections
            indent("Section Name:" + section.SectionInformation.Name)
        Next section
        indentLevel -= 1

        If (sectionGroup.SectionGroups.Count > 0) Then
            indent("Contained Section Groups:")

            indentLevel += 1
            Dim sectionGroups As ConfigurationSectionGroupCollection = _
                sectionGroup.SectionGroups
            ShowSectionGroupCollectionInfo(sectionGroups)
            indentLevel -= 1
        End If

        indent("")
        indentLevel -= 1

    End Sub
    Shared Sub indent(ByVal text As String)
        Dim i As Integer
        For i = 0 To indentLevel - 1
            Console.Write("  ")
        Next i
        Console.WriteLine(Left(text, 79 - indentLevel * 2))
    End Sub

End Class

注釈

構成ファイルの設定 (Web.config ファイルなど) は、セクションごとに構成されます。 一部のセクションは関連しているため、多くの場合、セクション グループにグループ化すると便利です。 クラスは ConfigurationSectionGroup 、構成ファイルの sectionGroup 要素で configSections セクションを定義するときにグループ化するために使用される XML 要素を表します。 セクション グループは入れ子にすることができます (セクション グループには、他のセクション グループとセクションを含めることができます)。 次の例は、 configSections 入れ子になった 3 つのセクション グループを定義する要素を示しています。

<configSections>
  <sectionGroup name="system.web.extensions"...>
    <sectionGroup name="scripting" ...>
      <section name="scriptResourceHandler".../>
      <sectionGroup name="webServices"...>
        <section name="jsonSerialization" .../>
        <section name="profileService" ... />        <section name="authenticationService" .../>
        <section name="roleService" .../>
      </sectionGroup>
    </sectionGroup>
  </sectionGroup>
</configSections>

構成システムは、構成ファイルからオブジェクトに設定を読み込 ConfigurationSectionGroup みます。 プロパティと SectionGroups プロパティをSections使用すると、オブジェクトに含まれるセクションとセクション グループにConfigurationSectionGroupアクセスできます。

構成ファイルから情報にアクセスする方法の詳細については、 クラスを ConfigurationManager 参照してください。

コンストラクター

ConfigurationSectionGroup()

ConfigurationSectionGroup クラスの新しいインスタンスを初期化します。

プロパティ

IsDeclarationRequired

この ConfigurationSectionGroup オブジェクト宣言が必要かどうかを示す値を取得します。

IsDeclared

ConfigurationSectionGroup オブジェクトが宣言されているかどうかを示す値を取得します。

Name

この ConfigurationSectionGroup オブジェクトの名前プロパティを取得します。

SectionGroupName

この ConfigurationSectionGroup に関連付けられているセクション グループ名を取得します。

SectionGroups

この ConfigurationSectionGroupCollection オブジェクトの子であるすべての ConfigurationSectionGroup オブジェクトを格納している ConfigurationSectionGroup オブジェクトを取得します。

Sections

この ConfigurationSectionCollection オブジェクト内のすべての ConfigurationSection オブジェクトを格納している ConfigurationSectionGroup オブジェクトを取得します。

Type

この ConfigurationSectionGroup オブジェクトの型を取得または設定します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
ForceDeclaration()

この ConfigurationSectionGroup オブジェクトの宣言を強制的に行います。

ForceDeclaration(Boolean)

この ConfigurationSectionGroup オブジェクトの宣言を強制的に行います。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ShouldSerializeSectionGroupInTargetVersion(FrameworkName)

指定したターゲット バージョンの.NET Frameworkに対して構成オブジェクト階層をシリアル化するときに、現在ConfigurationSectionGroupのインスタンスをシリアル化する必要があるかどうかを示します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください