ConfigurationPropertyAttribute Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Deklaratív módon utasítja .NET egy konfigurációs tulajdonság példányosítására. Ez az osztály nem örökölhető.
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
- Öröklődés
- Attribútumok
Példák
Az alábbi példa bemutatja, hogyan határozhatja meg egy egyéni ConfigurationSection objektum tulajdonságait az ConfigurationPropertyAttribute attribútum használatával.
A példa két osztályt tartalmaz. Az UrlsSection egyéni osztály a ConfigurationPropertyAttribute saját tulajdonságait határozza meg. Az UsingConfigurationPropertyAttribute osztály az UrlsSection egyéni szakasz olvasására és írására használja az alkalmazáskonfigurációs fájlban.
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
Az alábbiakban az előző mintában meghatározott egyéni szakaszt tartalmazó konfigurációs fájl részlete látható.
<?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>
Megjegyzések
A ConfigurationPropertyAttribute használatával díszíthet egy konfigurációs tulajdonságot, amely arra utasítja .NET, hogy a dekorálási paraméter értékével hozza létre és inicializálja a tulajdonságot.
Note
Az egyéni konfigurációelemek létrehozásának legegyszerűbb módja az attribútumként megadott (deklaratív) modell használata. Deklarálja az egyéni nyilvános tulajdonságokat, és díszítse őket az ConfigurationPropertyAttribute attribútummal. Az attribútummal megjelölt minden tulajdonság esetében .NET tükröződés használatával olvassa el a dekorálási paramétereket, és létrehoz egy kapcsolódó ConfigurationProperty példányt. A programozott modellt is használhatja, ebben az esetben az Ön feladata az egyéni nyilvános tulajdonságok deklarálása és a gyűjtemény visszaadása.
A .NET konfigurációs rendszer olyan attribútumtípusokat biztosít, amelyeket az egyéni konfigurációs elemek létrehozásakor használhat. Kétféle attribútumtípus létezik:
Az egyéni konfigurációelem-tulajdonságok példányosítására .NET típusok. Ezek a típusok a következők:
Az egyéni konfigurációelem tulajdonságainak ellenőrzésére .NET utasító típusok. Ezek a típusok a következők:
Konstruktorok
| Name | Description |
|---|---|
| ConfigurationPropertyAttribute(String) |
Inicializál egy új osztálypéldányt ConfigurationPropertyAttribute . |
Tulajdonságok
| Name | Description |
|---|---|
| DefaultValue |
Lekéri vagy beállítja a dekorált tulajdonság alapértelmezett értékét. |
| IsDefaultCollection |
Lekéri vagy beállít egy értéket, amely jelzi, hogy ez-e a dekorált konfigurációs tulajdonság alapértelmezett tulajdonsággyűjteménye. |
| IsKey |
Lekéri vagy beállít egy értéket, amely jelzi, hogy ez a dekorált elem tulajdonság kulcstulajdonság-e. |
| IsRequired |
Lekéri vagy beállít egy értéket, amely jelzi, hogy szükséges-e a dekorált elem tulajdonsága. |
| Name |
Lekéri a kitüntetett konfigurációelem-tulajdonság nevét. |
| Options |
Lekéri vagy beállítja a ConfigurationPropertyOptions dekorált konfigurációelem tulajdonságot. |
| TypeId |
Ha származtatott osztályban implementálják, ehhez egy egyedi azonosítót Attributekap. (Öröklődés forrása Attribute) |
Metódusok
| Name | Description |
|---|---|
| Equals(Object) |
Olyan értéket ad vissza, amely jelzi, hogy ez a példány egyenlő-e egy adott objektummal. (Öröklődés forrása Attribute) |
| GetHashCode() |
A példány kivonatkódját adja vissza. (Öröklődés forrása Attribute) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| IsDefaultAttribute() |
Ha egy származtatott osztályban felül van bírálva, azt jelzi, hogy a példány értéke-e a származtatott osztály alapértelmezett értéke. (Öröklődés forrása Attribute) |
| Match(Object) |
Származtatott osztály felülírásakor egy olyan értéket ad vissza, amely jelzi, hogy ez a példány egy adott objektummal egyenlő-e. (Öröklődés forrása Attribute) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |
Explicit interfész-implementációk
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Névkészletet képez le a küldési azonosítók megfelelő készletére. (Öröklődés forrása Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Lekéri egy objektum típusadatait, amelyek a felület típusadatainak lekérésére használhatók. (Öröklődés forrása Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Lekéri az objektumok által biztosított típusinformációs felületek számát (0 vagy 1). (Öröklődés forrása Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Hozzáférést biztosít az objektumok által közzétett tulajdonságokhoz és metódusokhoz. (Öröklődés forrása Attribute) |