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 条目注册其处理类型。 有关示例,请参阅示例部分中显示的配置文件摘录。

注意

在早期版本的 .NET Framework中,配置节处理程序用于以编程方式更改配置设置。 现在,所有默认配置节都由扩展类的 ConfigurationSection 类表示。

实施者说明

可以使用编程或声明性 (特性) 编码模型创建自定义配置节:

  • 编程模型。 此模型要求为每个节属性创建一个属性来获取或设置其值,并将其添加到基础 ConfigurationElement 基类的内部属性包。

  • 声明性模型。 此更简单的模型(也称为特性化模型)允许您通过使用属性并使用属性对其进行修饰来定义节属性。 这些属性指示 ASP.NET 配置系统有关属性类型及其默认值的信息。 利用通过反射获取的此信息,ASP.NET 配置系统将创建节属性对象并执行所需的初始化。

Configuration 允许以编程方式访问编辑配置文件。 可以访问这些文件进行读取或写入,如下所示:

  • 读取。 使用 GetSection(String)GetSectionGroup(String) 读取配置信息。 请注意,读取 的用户或进程必须具有以下权限:

    • 对当前配置层次结构级别的配置文件的读取权限。

    • 对所有父配置文件的读取权限。

    如果应用程序需要对其自己的配置进行只读访问,建议在 Web 应用程序中使用 GetSection 重载方法, GetSection(String) 在客户端应用程序中使用 方法。

    这些方法提供对当前应用程序的缓存配置值的访问权限,其性能比 Configuration 类更好。

注意:如果使用采用参数的path静态GetSection方法,则path参数必须引用运行代码的应用程序;否则,将忽略参数并返回当前正在运行的应用程序的配置信息。

  • 编写。 可以使用其中 Save 一种方法来写入配置信息。 请注意,写入的用户或进程必须具有以下权限:

    • 对当前配置层次结构级别的配置文件和目录的写入权限。

    • 对所有配置文件的读取权限。

构造函数

ConfigurationSection()

初始化 ConfigurationSection 类的新实例。

属性

CurrentConfiguration

获取对顶级 Configuration 实例的引用,该实例表示当前 ConfigurationElement 实例所属的配置层次结构。

(继承自 ConfigurationElement)
ElementInformation

获取包含 ConfigurationElement 对象的不可自定义的信息和功能的 ElementInformation 对象。

(继承自 ConfigurationElement)
ElementProperty

获取表示 ConfigurationElement 对象本身的 ConfigurationElementProperty 对象。

(继承自 ConfigurationElement)
EvaluationContext

获取 ConfigurationElement 对象的 ContextInformation 对象。

(继承自 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)

创建一个包含 ConfigurationSection 对象的分离视图的 XML 字符串,作为单独的节写入到文件中。

SerializeToXmlElement(XmlWriter, String)

当在派生类中实现后,将此配置元素的外部标记写入配置文件。

(继承自 ConfigurationElement)
SetPropertyValue(ConfigurationProperty, Object, Boolean)

将属性设置为指定值。

(继承自 ConfigurationElement)
SetReadOnly()

设置 ConfigurationElement 对象及所有子元素的 IsReadOnly() 属性。

(继承自 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)

适用于

另请参阅