ConfigurationPropertyAttribute Klas
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Declaratief geeft .NET opdracht om een configuratie-eigenschap te instantiëren. Deze klasse kan niet worden overgenomen.
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
- Overname
- Kenmerken
Voorbeelden
In het volgende voorbeeld ziet u hoe u de eigenschappen van een aangepast ConfigurationSection object definieert met behulp van het ConfigurationPropertyAttribute kenmerk.
Het voorbeeld bevat twee klassen. De UrlsSection aangepaste klasse maakt gebruik van de ConfigurationPropertyAttribute klasse om zijn eigen eigenschappen te definiëren. De UsingConfigurationPropertyAttribute klasse maakt gebruik van de UrlsSection klasse om de aangepaste sectie in het toepassingsconfiguratiebestand te lezen en te schrijven.
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
Hier volgt een fragment van het configuratiebestand met de aangepaste sectie, zoals gedefinieerd in het vorige voorbeeld.
<?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>
Opmerkingen
U gebruikt de ConfigurationPropertyAttribute om een configuratie-eigenschap te versieren, die .NET opdracht geeft om de eigenschap te instantiëren en te initialiseren met behulp van de waarde van de decoreringsparameter.
Note
De eenvoudigste manier om een aangepast configuratie-element te maken, is door het toegewezen (declaratieve) model te gebruiken. U declareert de aangepaste openbare eigenschappen en verfraaid met het ConfigurationPropertyAttribute kenmerk. Voor elke eigenschap die is gemarkeerd met dit kenmerk, gebruikt .NET weerspiegeling om de decoreringsparameters te lezen en een gerelateerd ConfigurationProperty-exemplaar te maken. U kunt ook het programmatische model gebruiken. In dat geval is het uw verantwoordelijkheid om de aangepaste openbare eigenschappen te declareren en hun verzameling te retourneren.
Het .NET configuratiesysteem biedt kenmerktypen die u kunt gebruiken tijdens het maken van aangepaste configuratie-elementen. Er zijn twee soorten kenmerktypen:
De typen geven .NET instructies voor het instantiëren van de eigenschappen van het aangepaste configuratie-element. Deze typen omvatten:
De typen die .NET instructies geven voor het valideren van de eigenschappen van het aangepaste configuratie-element. Deze typen omvatten:
Constructors
| Name | Description |
|---|---|
| ConfigurationPropertyAttribute(String) |
Initialiseert een nieuw exemplaar van ConfigurationPropertyAttribute klasse. |
Eigenschappen
| Name | Description |
|---|---|
| DefaultValue |
Hiermee haalt u de standaardwaarde voor de gedecoreerde eigenschap op of stelt u deze in. |
| IsDefaultCollection |
Hiermee wordt een waarde opgehaald of ingesteld die aangeeft of dit de standaardeigenschapsverzameling is voor de ingerichte configuratie-eigenschap. |
| IsKey |
Hiermee wordt een waarde opgehaald of ingesteld die aangeeft of dit een sleuteleigenschap is voor de ingerichte elementeigenschap. |
| IsRequired |
Hiermee wordt een waarde opgehaald of ingesteld die aangeeft of de eigenschap van het ingerichte element vereist is. |
| Name |
Hiermee haalt u de naam op van de ingerichte eigenschap van het configuratie-element. |
| Options |
Hiermee haalt u de eigenschap voor het ingerichte configuratie-element op of stelt u deze ConfigurationPropertyOptions in. |
| TypeId |
Wanneer deze wordt geïmplementeerd in een afgeleide klasse, krijgt u Attributehiervoor een unieke id. (Overgenomen van Attribute) |
Methoden
| Name | Description |
|---|---|
| Equals(Object) |
Retourneert een waarde die aangeeft of dit exemplaar gelijk is aan een opgegeven object. (Overgenomen van Attribute) |
| GetHashCode() |
Retourneert de hash-code voor dit exemplaar. (Overgenomen van Attribute) |
| GetType() |
Hiermee haalt u de Type huidige instantie op. (Overgenomen van Object) |
| IsDefaultAttribute() |
Wanneer deze wordt overschreven in een afgeleide klasse, geeft u aan of de waarde van dit exemplaar de standaardwaarde is voor de afgeleide klasse. (Overgenomen van Attribute) |
| Match(Object) |
Wanneer deze wordt overschreven in een afgeleide klasse, wordt een waarde geretourneerd die aangeeft of dit exemplaar gelijk is aan een opgegeven object. (Overgenomen van Attribute) |
| MemberwiseClone() |
Hiermee maakt u een ondiepe kopie van de huidige Object. (Overgenomen van Object) |
| ToString() |
Retourneert een tekenreeks die het huidige object vertegenwoordigt. (Overgenomen van Object) |
Expliciete interface-implementaties
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Hiermee wordt een set namen toegewezen aan een bijbehorende set verzend-id's. (Overgenomen van Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Hiermee haalt u de typegegevens voor een object op, die kan worden gebruikt om de typegegevens voor een interface op te halen. (Overgenomen van Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Hiermee wordt het aantal type-informatieinterfaces opgehaald dat een object biedt (0 of 1). (Overgenomen van Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Biedt toegang tot eigenschappen en methoden die door een object worden weergegeven. (Overgenomen van Attribute) |