ConfigurationSectionGroup 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示配置文件中的一组相关节。
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
嵌套节组的元素:
<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 。 可以使用 Sections 和 SectionGroups 属性访问对象中包含的 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) |