ConfigurationPropertyAttribute Class
Assembly: System.Configuration (in system.configuration.dll)
'Declaration <AttributeUsageAttribute(AttributeTargets.Property)> _ Public NotInheritable Class ConfigurationPropertyAttribute Inherits Attribute 'Usage Dim instance As ConfigurationPropertyAttribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Property) */ public final class ConfigurationPropertyAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property) public final class ConfigurationPropertyAttribute extends Attribute
Not applicable.
You use the ConfigurationPropertyAttribute to decorate a configuration property, which will instruct the .NET Framework to instantiate and to initialize the property using the value of the decorating parameter.
Note: |
|---|
| The simplest way to create a custom configuration element is to use the attributed (declarative) model. You declare the custom public properties and decorate them with the ConfigurationPropertyAttribute attribute. For each property marked with this attribute, the .NET Framework uses reflection to read the decorating parameters and create a related ConfigurationProperty instance. You can also use the programmatic model, in which case it is your responsibility to declare the custom public properties and return their collection. |
The .NET Framework configuration system provides attribute types that you can use during the creation of custom configuration elements. There are two kinds of attribute types:
-
The types instructing the .NET Framework how to instantiate the custom configuration-element properties. These types include:
-
ConfigurationPropertyAttribute
-
The types instructing the .NET Framework how to validate the custom configuration-element properties. These types include:
The following example shows how to decorate the properties of a custom ConfigurationSection object using the ConfigurationPropertyAttribute attribute.
Public Class SampleSection Inherits ConfigurationSection Public Sub New() End Sub 'New <ConfigurationProperty("fileName", _ DefaultValue:="default.txt", _ IsRequired:=True, _ IsKey:=False), _ 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 <ConfigurationProperty("maxUsers", _ DefaultValue:=10000, _ IsRequired:=False), _ LongValidator(MinValue:=1, _ MaxValue:=10000000, _ 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 <ConfigurationProperty("maxIdleTime", _ DefaultValue:="0:10:0", _ IsRequired:=False), _ 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 'SampleSection
The following is an excerpt of the configuration file containing the custom section as defined in the previous sample.
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="custom" type="Samples.AspNet.SampleSection, ConfigurationPropertyAttribute" />
</configSections>
<custom fileName="Default.txt"
maxUsers="2500" maxIdleTime="00:10:00" />
</configuration>
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: