ConfigurationSectionGroup Klasa

Definicja

Reprezentuje grupę powiązanych sekcji w pliku konfiguracji.

public ref class ConfigurationSectionGroup
public class ConfigurationSectionGroup
type ConfigurationSectionGroup = class
Public Class ConfigurationSectionGroup
Dziedziczenie
ConfigurationSectionGroup
Pochodne

Przykłady

W poniższym przykładzie pokazano, jak używać klasy do pobierania ConfigurationSectionGroup ustawień konfiguracji. Przykładem jest aplikacja konsolowa, która odczytuje ustawienia konfiguracji i zapisuje informacje o każdej grupie sekcji konfiguracji oraz sekcjach w niej w konsoli programu .

Metoda Main ładuje ustawienia konfiguracji do Configuration obiektu, pobiera SectionGroups kolekcję z Configuration obiektu, a następnie wywołuje ShowSectionGroupCollectionInfo metodę w celu wyświetlenia wartości właściwości sekcji.

Metoda ShowSectionGroupCollectionInfo wykonuje iterację w grupach sekcji i wywołuje metodę ShowSectionGroupInfo dla każdego z nich.

Metoda ShowSectionGroupInfo wyświetla nazwę grupy sekcji, niektóre wartości właściwości i nazwy sekcji, które zawiera. Jeśli grupa sekcji zawiera grupy sekcji, ta metoda wywołuje ShowSectionGroupCollectionInfo cyklicznie, aby wyświetlić te grupy sekcji.

Pole indentLevel służy do dodawania spacji po lewej stronie wyświetlanych wierszy w celu wyświetlania grup logicznych. Wszystkie wiersze są ograniczone do 79 znaków tekstu, aby uniknąć zawijania wierszy, co utrudnia rozróżnienie grup logicznych.

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

Uwagi

Ustawienia w plikach konfiguracji (takich jak plik Web.config) są uporządkowane w sekcjach. Ponieważ niektóre sekcje są powiązane, często wygodne jest grupowanie ich w grupie sekcji. Klasa ConfigurationSectionGroup reprezentuje sectionGroup element XML używany do grupowania sekcji podczas ich definiowania w configSections elemecie pliku konfiguracji. Grupy sekcji można zagnieżdżać (grupa sekcji może zawierać inne grupy sekcji, a także sekcje). W poniższym przykładzie przedstawiono configSections element definiujący trzy zagnieżdżone grupy sekcji:

<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>

System konfiguracji ładuje ustawienia z plików konfiguracji do ConfigurationSectionGroup obiektów. Właściwości i SectionGroups umożliwiają Sections uzyskiwanie dostępu do sekcji i grup sekcji zawartych w ConfigurationSectionGroup obiekcie.

Aby uzyskać więcej informacji na temat uzyskiwania dostępu do informacji z plików konfiguracji, zobacz klasę ConfigurationManager .

Konstruktory

ConfigurationSectionGroup()

Inicjuje nowe wystąpienie klasy ConfigurationSectionGroup.

Właściwości

IsDeclarationRequired

Pobiera wartość wskazującą, czy ta ConfigurationSectionGroup deklaracja obiektu jest wymagana.

IsDeclared

Pobiera wartość wskazującą, czy ten ConfigurationSectionGroup obiekt jest zadeklarowany.

Name

Pobiera właściwość name tego ConfigurationSectionGroup obiektu.

SectionGroupName

Pobiera nazwę grupy sekcji skojarzona z tym ConfigurationSectionGroupelementem .

SectionGroups

ConfigurationSectionGroupCollection Pobiera obiekt zawierający wszystkie ConfigurationSectionGroup obiekty podrzędne tego ConfigurationSectionGroup obiektu.

Sections

ConfigurationSectionCollection Pobiera obiekt zawierający wszystkie ConfigurationSection obiekty w tym ConfigurationSectionGroup obiekcie.

Type

Pobiera lub ustawia typ tego ConfigurationSectionGroup obiektu.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
ForceDeclaration()

Wymusza deklarację dla tego ConfigurationSectionGroup obiektu.

ForceDeclaration(Boolean)

Wymusza deklarację dla tego ConfigurationSectionGroup obiektu.

GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ShouldSerializeSectionGroupInTargetVersion(FrameworkName)

Wskazuje, czy bieżące ConfigurationSectionGroup wystąpienie powinno być serializowane, gdy hierarchia obiektów konfiguracji jest serializowana dla określonej wersji docelowej .NET Framework.

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też