ConfigurationPropertyAttribute 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
以声明方式指示 .NET 实例化配置属性。 此类不能被继承。
public ref class ConfigurationPropertyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class ConfigurationPropertyAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property)>]
type ConfigurationPropertyAttribute = class
inherit Attribute
Public NotInheritable Class ConfigurationPropertyAttribute
Inherits Attribute
- 继承
- 属性
示例
以下示例演示如何使用 ConfigurationPropertyAttribute 特性定义自定义ConfigurationSection对象的属性。
该示例包含两个类。 自定义 UrlsSection
类使用 ConfigurationPropertyAttribute 来定义其自己的属性。 类 UsingConfigurationPropertyAttribute
使用 UrlsSection
读取和写入应用程序配置文件中的自定义节。
using System;
using System.Configuration;
// Define a custom section.
// This class shows how to use the ConfigurationPropertyAttribute.
public class UrlsSection : ConfigurationSection
{
[ConfigurationProperty("name", DefaultValue = "Contoso",
IsRequired = true, IsKey = true)]
public string Name
{
get
{
return (string)this["name"];
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("url", DefaultValue = "http://www.contoso.com",
IsRequired = true)]
[RegexStringValidator(@"\w+:\/\/[\w.]+\S*")]
public string Url
{
get
{
return (string)this["url"];
}
set
{
this["url"] = value;
}
}
[ConfigurationProperty("port", DefaultValue = (int)0, IsRequired = false)]
[IntegerValidator(MinValue = 0, MaxValue = 8080, ExcludeRange = false)]
public int Port
{
get
{
return (int)this["port"];
}
set
{
this["port"] = value;
}
}
}
Imports System.Configuration
' Define a custom section.
' This class shows how to use the ConfigurationPropertyAttribute.
Public Class UrlsSection
Inherits ConfigurationSection
<ConfigurationProperty("name", DefaultValue:="Contoso", IsRequired:=True, IsKey:=True)>
Public Property Name() As String
Get
Return CStr(Me("name"))
End Get
Set(ByVal value As String)
Me("name") = value
End Set
End Property
<ConfigurationProperty("url", DefaultValue:="http://www.contoso.com", IsRequired:=True), RegexStringValidator("\w+:\/\/[\w.]+\S*")>
Public Property Url() As String
Get
Return CStr(Me("url"))
End Get
Set(ByVal value As String)
Me("url") = value
End Set
End Property
<ConfigurationProperty("port", DefaultValue:=0, IsRequired:=False), IntegerValidator(MinValue:=0, MaxValue:=8080, ExcludeRange:=False)>
Public Property Port() As Integer
Get
Return CInt(Fix(Me("port")))
End Get
Set(ByVal value As Integer)
Me("port") = value
End Set
End Property
End Class
using System;
using System.Configuration;
public class UsingConfigurationPropertyAttribute
{
// Create a custom section and save it in the
// application configuration file.
static void CreateCustomSection()
{
try
{
// Create a custom configuration section.
UrlsSection customSection = new UrlsSection();
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Add the custom section to the application
// configuration file.
if (config.Sections["CustomSection"] == null)
{
config.Sections.Add("CustomSection", customSection);
}
// Save the application configuration file.
customSection.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
Console.WriteLine("Created custom section in the application configuration file: {0}",
config.FilePath);
Console.WriteLine();
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine("CreateCustomSection: {0}", err.ToString());
}
}
static void ReadCustomSection()
{
try
{
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None) as Configuration;
// Read and display the custom section.
UrlsSection customSection =
config.GetSection("CustomSection") as UrlsSection;
Console.WriteLine("Reading custom section from the configuration file.");
Console.WriteLine("Section name: {0}", customSection.Name);
Console.WriteLine("Url: {0}", customSection.Url);
Console.WriteLine("Port: {0}", customSection.Port);
Console.WriteLine();
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine("ReadCustomSection(string): {0}", err.ToString());
}
}
static void Main(string[] args)
{
// Get the name of the application.
string appName =
Environment.GetCommandLineArgs()[0];
// Create a custom section and save it in the
// application configuration file.
CreateCustomSection();
// Read the custom section saved in the
// application configuration file.
ReadCustomSection();
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
Imports System.Configuration
Public Class UsingConfigurationPropertyAttribute
' Create a custom section and save it in the
' application configuration file.
Private Shared Sub CreateCustomSection()
Try
' Create a custom configuration section.
Dim customSection As New UrlsSection()
' Get the current configuration file.
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
' Add the custom section to the application
' configuration file.
If config.Sections("CustomSection") Is Nothing Then
config.Sections.Add("CustomSection", customSection)
End If
' Save the application configuration file.
customSection.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Modified)
Console.WriteLine("Created custom section in the application configuration file: {0}", config.FilePath)
Console.WriteLine()
Catch err As ConfigurationErrorsException
Console.WriteLine("CreateCustomSection: {0}", err.ToString())
End Try
End Sub
Private Shared Sub ReadCustomSection()
Try
' Get the application configuration file.
Dim config As System.Configuration.Configuration = TryCast(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None), Configuration)
' Read and display the custom section.
Dim customSection As UrlsSection = TryCast(config.GetSection("CustomSection"), UrlsSection)
Console.WriteLine("Reading custom section from the configuration file.")
Console.WriteLine("Section name: {0}", customSection.Name)
Console.WriteLine("Url: {0}", customSection.Url)
Console.WriteLine("Port: {0}", customSection.Port)
Console.WriteLine()
Catch err As ConfigurationErrorsException
Console.WriteLine("ReadCustomSection(string): {0}", err.ToString())
End Try
End Sub
Shared Sub Main(ByVal args() As String)
' Get the name of the application.
Dim appName As String = Environment.GetCommandLineArgs()(0)
' Create a custom section and save it in the
' application configuration file.
CreateCustomSection()
' Read the custom section saved in the
' application configuration file.
ReadCustomSection()
Console.WriteLine("Press enter to exit.")
Console.ReadLine()
End Sub
End Class
下面是配置文件的摘录,其中包含上一示例中定义的自定义节。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="CustomSection" type="UrlsSection, UsingConfigurationPropertyAttribute, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<CustomSection name="Contoso" url="http://www.contoso.com" />
</configuration>
注解
使用 ConfigurationPropertyAttribute 来修饰配置属性,这将指示 .NET 实例化并使用修饰参数的值初始化属性。
注意
创建自定义配置元素的最简单方法是使用特性化 (声明性) 模型。 声明自定义公共属性并使用 属性修饰它们 ConfigurationPropertyAttribute 。 对于使用此属性标记的每个属性,.NET 使用反射来读取修饰参数并创建相关 ConfigurationProperty 实例。 还可以使用编程模型,在这种情况下,由你负责声明自定义公共属性并返回其集合。
.NET 配置系统提供可在创建自定义配置元素期间使用的属性类型。 有两种类型的属性类型:
指示 .NET 如何实例化自定义配置元素属性的类型。 这些类型包括:
指示 .NET 如何验证自定义配置元素属性的类型。 这些类型包括:
构造函数
ConfigurationPropertyAttribute(String) |
初始化 ConfigurationPropertyAttribute 类的新实例。 |
属性
DefaultValue |
获取或设置修饰属性的默认值。 |
IsDefaultCollection |
获取或设置一个值,指示此属性集合是否为经过修饰的配置属性的默认属性集合。 |
IsKey |
获取或设置一个值,该值指示此属性是否是该经过修饰的元素属性的 Key 属性。 |
IsRequired |
获取或设置一个值,该值指示经过修饰的元素属性是否为必需。 |
Name |
获取经过修饰的配置元素属性的名称。 |
Options |
为经过修饰的配置元素属性获取或设置 ConfigurationPropertyOptions。 |
TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。 (继承自 Attribute) |
方法
Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute) |
GetHashCode() |
返回此实例的哈希代码。 (继承自 Attribute) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute) |
Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
显式接口实现
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。 (继承自 Attribute) |