ConfigurationSection 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
구성 파일 내의 섹션을 나타냅니다.
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
- 상속
- 파생
예제
다음 예제에서는 프로그래밍 방식으로 사용자 지정 섹션을 구현하는 방법을 보여 줍니다.
특성 모델을 사용하여 구현된 사용자 지정 섹션을 구현하고 사용하는 방법을 보여 주는 전체 예제는 다음을 참조하세요 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 대한 사용자 지정 처리 및 프로그래밍 방식 액세스를 제공하도록 클래스를 확장합니다.
섹션은 해당 처리 형식을 요소의 항목에 등록합니다 configSections . 예를 들어 예제 섹션에 표시된 구성 파일 발췌를 참조하세요.
메모
이전 버전의 .NET Framework에서는 구성 섹션 처리기를 사용하여 구성 설정을 프로그래밍 방식으로 변경했습니다. 이제 모든 기본 구성 섹션은 클래스를 확장하는 ConfigurationSection 클래스로 표시됩니다.
구현자 참고
프로그래밍 방식 또는 선언적(특성 지정된) 코딩 모델을 사용하여 사용자 지정 구성 섹션을 만들 수 있습니다.
프로그래밍 방식 모델입니다. 이 모델을 사용하려면 각 섹션 특성에 대해 속성을 만들어 해당 값을 가져와서 기본 기본 클래스의 내부 속성 모음에 ConfigurationElement 추가해야 합니다.
선언적 모델입니다. 특성 모델이라고도 하는 이 간단한 모델을 사용하면 속성을 사용하고 특성으로 데코레이팅하여 섹션 특성을 정의할 수 있습니다. 이러한 특성은 속성 형식 및 해당 기본값에 대해 ASP.NET 구성 시스템에 지시합니다. 리플렉션을 통해 얻은 이 정보를 사용하여 ASP.NET 구성 시스템은 섹션 속성 개체를 만들고 필요한 초기화를 수행합니다.
이 Configuration 클래스는 구성 파일을 편집하기 위한 프로그래밍 방식 액세스를 허용합니다. 다음과 같이 읽기 또는 쓰기를 위해 이러한 파일에 액세스할 수 있습니다.
읽기. 구성 정보를 사용 GetSection(String) 하거나 GetSectionGroup(String) 읽습니다. 읽는 사용자 또는 프로세스에는 다음 권한이 있어야 합니다.
현재 구성 계층 수준에서 구성 파일에 대한 읽기 권한입니다.
모든 부모 구성 파일에 대한 읽기 권한입니다.
애플리케이션이 자체 구성에 대한 읽기 전용 액세스가 필요한 경우 웹 애플리케이션의 경우 오버로드된 메서드 또는 GetSection(String) 클라이언트 애플리케이션의 경우 메서드를 사용하는 GetSection 것이 좋습니다.
이러한 메서드는 현재 애플리케이션에 대해 캐시된 구성 값에 대한 액세스를 제공하며, 클래스보다 성능이 Configuration 향상됩니다.
참고: 매개 변수 path 를 사용하는 path 정적 GetSection 메서드를 사용하는 경우 매개 변수는 코드가 실행 중인 애플리케이션을 참조해야 합니다. 그렇지 않으면 매개 변수가 무시되고 현재 실행 중인 애플리케이션에 대한 구성 정보가 반환됩니다.
쓰기. 메서드 중 Save 하나를 사용하여 구성 정보를 작성합니다. 작성하는 사용자 또는 프로세스에는 다음 권한이 있어야 합니다.
현재 구성 계층 수준에서 구성 파일 및 디렉터리에 대한 쓰기 권한입니다.
모든 구성 파일에 대한 읽기 권한입니다.
생성자
| Name | Description |
|---|---|
| ConfigurationSection() |
ConfigurationSection 클래스의 새 인스턴스를 초기화합니다. |