System.Configuration.ConfigurationSectionGroup 类

方法描述

表示配置文件中的一组相关节。

语法定义(C# System.Configuration.ConfigurationSectionGroup 类 的用法)

public class ConfigurationSectionGroup

构造函数

构造函数名称 构造函数描述
ConfigurationSectionGroup 初始化 ConfigurationSectionGroup 类的新实例。

成员/方法

方法名称 方法描述
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
ForceDeclaration() 强制声明此 ConfigurationSectionGroup 对象。
ForceDeclaration(Boolean) 强制声明此 ConfigurationSectionGroup 对象。
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetType 获取当前实例的 Type。 (继承自 Object。)
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
ShouldSerializeSectionGroupInTargetVersion 指示在为指定目标版本的 .NET Framework 序列化配置对象层次结构时是否应序列化当前的 ConfigurationSectionGroup 实例。
ToString 返回表示当前对象的字符串。 (继承自 Object。)

提示和注释

配置文件(如 Web.config 文件)中的设置被组织成节。 因为有些节是相关的,所以将它们分组到节组中通常是很方便的做法。 ConfigurationSectionGroup 类表示在配置文件的 configSections 元素中定义节时,用于对节进行分组的 sectionGroup XML 元素。 节组可以嵌套(节组可以包含其他节组以及节)。 下面的示例演示一个定义三个嵌套节组的 configSections 元素:

复制

配置系统将设置从配置文件加载到 ConfigurationSectionGroup 对象中。 可以使用 Sections 和 SectionGroups 属性,以访问 ConfigurationSectionGroup 对象中包含的节和节组。

有关如何从配置文件中访问信息的更多信息,请参见 ConfigurationManager 类。

System.Configuration.ConfigurationSectionGroup 类例子

所有行均限制为 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)));
        }
    }

}

继承层次结构

System.Object

System.Configuration.ConfigurationSectionGroup

更多...

命名空间

namespace: System.Configuration

程序集: System.Configuration(在 System.Configuration.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。