ConfigurationProperty 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表設定元素的屬性或子節點。 此類別無法獲得繼承。
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。
public ref class ConfigurationProperty sealed
public sealed class ConfigurationProperty
type ConfigurationProperty = class
Public NotInheritable Class ConfigurationProperty
- 繼承
-
ConfigurationProperty
範例
- 以下程式碼範例展示了在建立自訂區段時如何使用 。ConfigurationProperty
using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
namespace ConfigurationPropertyExample
{
// Define a custom section.
// Shows how to use the ConfigurationProperty
// class when defining a custom section.
public sealed class CustomSection : ConfigurationSection
{
// The collection (property bag) that contains
// the section properties.
private static ConfigurationPropertyCollection _Properties;
// The FileName property.
private static ConfigurationProperty _FileName;
// The Alias property.
private static ConfigurationProperty _Alias;
// The MaxUsers property.
private static ConfigurationProperty _MaxUsers;
// The MaxIdleTime property.
private static ConfigurationProperty _MaxIdleTime;
// CustomSection constructor.
static CustomSection()
{
// Initialize the _FileName property
_FileName =
new ConfigurationProperty("fileName",
typeof(string), "default.txt");
// Initialize the _MaxUsers property
_MaxUsers =
new ConfigurationProperty("maxUsers",
typeof(long), (long)1000,
ConfigurationPropertyOptions.None);
// Initialize the _MaxIdleTime property
TimeSpan minTime = TimeSpan.FromSeconds(30);
TimeSpan maxTime = TimeSpan.FromMinutes(5);
ConfigurationValidatorBase _TimeSpanValidator =
new TimeSpanValidator(minTime, maxTime, false);
_MaxIdleTime =
new ConfigurationProperty("maxIdleTime",
typeof(TimeSpan), TimeSpan.FromMinutes(5),
TypeDescriptor.GetConverter(typeof(TimeSpan)),
_TimeSpanValidator,
ConfigurationPropertyOptions.IsRequired,
"[Description:This is the max idle time.]");
// Initialize the _Alias property
_Alias =
new ConfigurationProperty("alias",
typeof(string), "alias.txt");
// Initialize the Property collection.
_Properties = new ConfigurationPropertyCollection();
_Properties.Add(_FileName);
_Properties.Add(_Alias);
_Properties.Add(_MaxUsers);
_Properties.Add(_MaxIdleTime);
}
// Return the initialized property bag
// for the configuration element.
protected override ConfigurationPropertyCollection Properties
{
get
{
return _Properties;
}
}
// Clear the property.
public void ClearCollection()
{
Properties.Clear();
}
// Remove an element from the property collection.
public void RemoveCollectionElement(string elName)
{
Properties.Remove(elName);
}
// Get the property collection enumerator.
public IEnumerator GetCollectionEnumerator()
{
return (Properties.GetEnumerator());
}
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public string FileName
{
get
{
return (string)this["fileName"];
}
set
{
this["fileName"] = value;
}
}
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public string Alias
{
get
{
return (string)this["alias"];
}
set
{
this["alias"] = value;
}
}
[LongValidator(MinValue = 1, MaxValue = 1000000,
ExcludeRange = false)]
public long MaxUsers
{
get
{
return (long)this["maxUsers"];
}
set
{
this["maxUsers"] = value;
}
}
public TimeSpan MaxIdleTime
{
get
{
return (TimeSpan)this["maxIdleTime"];
}
set
{
this["maxIdleTime"] = value;
}
}
}
}
Imports System.Configuration
Imports System.Collections
Imports System.ComponentModel
' Define a custom section.
' Shows how to use the ConfigurationProperty
' class when defining a custom section.
Public NotInheritable Class CustomSection
Inherits ConfigurationSection
' The collection (property bag) that contains
' the section properties.
Private Shared _Properties As ConfigurationPropertyCollection
' The FileName property.
Private Shared _FileName As ConfigurationProperty
' The Alias property.
Private Shared _Alias As ConfigurationProperty
' The MasUsers property.
Private Shared _MaxUsers As ConfigurationProperty
' The MaxIdleTime property.
Private Shared _MaxIdleTime As ConfigurationProperty
' CustomSection constructor.
Shared Sub New()
' Initialize the _FileName property
_FileName = New ConfigurationProperty( _
"fileName", GetType(String), "default.txt")
' Initialize the _MaxUsers property
_MaxUsers = New ConfigurationProperty( _
"maxUsers", GetType(Long), 1000L, _
ConfigurationPropertyOptions.None)
' Initialize the _MaxIdleTime property
Dim minTime As TimeSpan = TimeSpan.FromSeconds(30)
Dim maxTime As TimeSpan = TimeSpan.FromMinutes(5)
Dim _TimeSpanValidator = _
New TimeSpanValidator(minTime, maxTime, False)
_MaxIdleTime = New ConfigurationProperty( _
"maxIdleTime", GetType(TimeSpan), _
TimeSpan.FromMinutes(5), _
TypeDescriptor.GetConverter(GetType(TimeSpan)), _
_TimeSpanValidator, _
ConfigurationPropertyOptions.IsRequired, _
"[Description:This is the max idle time.]")
' Initialize the _Alias property
_Alias = New ConfigurationProperty( _
"alias", GetType(String), "alias.txt")
' Property collection initialization.
' The collection (property bag) that contains
' the properties is declared as:
' ConfigurationPropertyCollection _Properties;
_Properties = New ConfigurationPropertyCollection()
_Properties.Add(_FileName)
_Properties.Add(_Alias)
_Properties.Add(_MaxUsers)
_Properties.Add(_MaxIdleTime)
End Sub
' Return the initialized property bag
' for the configuration element.
Protected Overrides ReadOnly Property Properties() _
As ConfigurationPropertyCollection
Get
Return _Properties
End Get
End Property
<StringValidator(InvalidCharacters:= _
" ~!@#$%^&*()[]{}/;'""|\", MinLength:=1, _
MaxLength:=60)> _
Public Property FileName() As String
Get
Return CStr(Me("fileName"))
End Get
Set(ByVal value As String)
Me("fileName") = value
End Set
End Property
<StringValidator(InvalidCharacters:= _
" ~!@#$%^&*()[]{}/;'""|\", MinLength:=1, _
MaxLength:=60)> _
Public Property [Alias]() As String
Get
Return CStr(Me("alias"))
End Get
Set(ByVal value As String)
Me("alias") = 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
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
以下是前述範例中使用的設定檔摘錄。
<configuration>
<configSections>
<section name="CustomSection" type="ConfigurationPropertyExample.CustomSection, ConfigurationPropertyExample"
allowDefinition="Everywhere" allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<CustomSection fileName="override.txt" alias="alias.txt"
maxUsers="1000" maxIdleTime="00:05:00" />
</configuration>
以下範例說明如何在程式碼中建立前一個區段。
// Define a custom section programmatically.
static void CreateSection()
{
try
{
CustomSection customSection;
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Create the section entry
// in the <configSections> and the
// related target section in <configuration>.
// Call it "CustomSection2" since the file in this
// example already has "CustomSection".
if (config.Sections["CustomSection"] == null)
{
customSection = new CustomSection();
config.Sections.Add("CustomSection2", customSection);
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine(err.ToString());
}
}
' Create a custom section.
Shared Sub CreateSection()
Try
Dim customSection As CustomSection
' Get the current configuration file.
Dim config As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Create the section entry
' in the <configSections> and the
' related target section in <configuration>.
' Since the config file already has "CustomSection",
' call this one "CustomSection2".
If config.Sections("CustomSection") Is Nothing Then
customSection = New CustomSection()
config.Sections.Add("CustomSection", customSection)
customSection.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Full)
End If
Catch err As ConfigurationErrorsException
Console.WriteLine(err.ToString())
End Try
End Sub
備註
在簡單 ConfigurationElement的情況下,如 CustomSection 下一個例子所示,物件 ConfigurationProperty 代表屬性,如 fileName。
在較複雜的配置元素中,例如包含子區段 authentication的區段,這些 ConfigurationProperty 物件可以表示 ConfigurationElement 物件以及屬性。
該 ConfigurationPropertyCollection 類別代表一組 ConfigurationProperty 可以是屬性或 ConfigurationElement 配置元素的物件集合。
該 ConfigurationProperty 類別代表一個個別的配置設定。 這個類別允許你取得或設定特定組態實體(屬性或元素)的名稱、型別和預設值,並指定該屬性是必需的、是元素鍵,還是代表預設元素集合。
給繼承者的注意事項
每個 ConfigurationElement 物件都會建立一個內部 ConfigurationPropertyCollection 物件集合 ConfigurationProperty ,代表元素屬性或子元素集合。
不可自訂的資訊與功能由 ElementInformation 屬性提供的 ElementInformation 物件所包含。
您可以使用程式化或宣告式(有屬性)編碼模型來建立自訂的配置元素。
程式化模型。 這個模型需要你為每個元素屬性建立一個屬性,才能取得和/或設定其值,並將其加入底層 ConfigurationElement 基底類別的內部屬性袋中。
宣告式模型。 這個較簡單的模型,也稱為屬性模型,允許你透過屬性定義元素屬性,並用屬性裝飾它。 這些屬性指示 ASP.NET 配置系統關於屬性類型及其預設值。 透過反射獲得的資訊,ASP.NET 組態系統會為你建立元素屬性物件並執行必要的初始化。
建構函式
| 名稱 | Description |
|---|---|
| ConfigurationProperty(String, Type, Object, ConfigurationPropertyOptions) |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 初始化 ConfigurationProperty 類別的新執行個體。 |
| ConfigurationProperty(String, Type, Object, TypeConverter, ConfigurationValidatorBase, ConfigurationPropertyOptions, String) |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 初始化 ConfigurationProperty 類別的新執行個體。 |
| ConfigurationProperty(String, Type, Object, TypeConverter, ConfigurationValidatorBase, ConfigurationPropertyOptions) |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 初始化 ConfigurationProperty 類別的新執行個體。 |
| ConfigurationProperty(String, Type, Object) |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 初始化 ConfigurationProperty 類別的新執行個體。 |
| ConfigurationProperty(String, Type) |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 初始化 ConfigurationProperty 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| Converter |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 用 TypeConverter 來將此 ConfigurationProperty 轉換成 XML 表示,以便寫入設定檔。 |
| DefaultValue |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 取得該 ConfigurationProperty 屬性的預設值。 |
| Description |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 會獲得與 ConfigurationProperty. 相關聯的描述。 |
| IsAssemblyStringTransformationRequired |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 表示組態屬性的組合名稱在序列化至早期版本的 .NET 框架時是否需要轉換。 |
| IsDefaultCollection |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 會得到一個值,表示該屬性是否是元素的預設集合。 |
| IsKey |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 會得到一個值,表示該 ConfigurationProperty 物件是否為該物件 ConfigurationElement 的鍵。 |
| IsRequired |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 會得到一個值,表示是否需要這麼做 ConfigurationProperty 。 |
| IsTypeStringTransformationRequired |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 表示在序列化至早期版本的 .NET 框架時,配置屬性的型別名稱是否需要轉換。 |
| IsVersionCheckRequired |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 表示是否在序列化時查詢設定屬性的父組態區段,以判斷是否應序列化為 XML。 |
| Name |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 知道這個 ConfigurationProperty名字。 |
| Type |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 能理解這個 ConfigurationProperty 物件的類型。 |
| Validator |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 得到 ConfigurationValidatorAttribute,用來驗證此 ConfigurationProperty 物件。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 傳回表示目前 物件的字串。 (繼承來源 Object) |