ConfigurationCollectionAttribute クラス
アセンブリ: System.Configuration (system.configuration.dll 内)

<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Property)> _ Public NotInheritable Class ConfigurationCollectionAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Property)] public sealed class ConfigurationCollectionAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Property)] public ref class ConfigurationCollectionAttribute sealed : public Attribute

ConfigurationCollectionAttribute 属性を使用して、ConfigurationElementCollection 要素を装飾します。これにより、コレクションをインスタンス化し、カスタムの ConfigurationElement 値を使用して初期化するように .NET Framework に指示します。
![]() |
---|
カスタムの構成要素を作成する最も簡単な方法は、属性付き (宣言) モデルを使用することです。要素を宣言し、それらを ConfigurationCollectionAttribute 属性で装飾します。.NET Framework は、この属性でマークされた要素ごとに、リフレクションを使用して装飾のパラメータを読み取り、関連する ConfigurationElementCollection インスタンスを作成します。また、プログラム モデルも使用できます。この場合には、開発者が、カスタムのパブリック コレクションを宣言するほかに、ConfigurationElementCollection メンバをオーバーライドし、プロパティのコレクションを返す必要があります。 |
.NET Framework 構成システムには、カスタムの構成要素の作成時に使用できる属性の型が用意されています。2 種類の属性があります。
-
.NET Framework に、カスタムの構成要素のプロパティをインスタンス化する方法を指示する属性。次の属性がこれらの型に含まれます。
-
ConfigurationCollectionAttribute
-
ConfigurationPropertyAttribute
-
-
.NET Framework に、カスタムの構成要素のプロパティを検証する方法を指示する属性。次の属性がこれらの型に含まれます。
-
IntegerValidatorAttribute
-
LongValidatorAttribute
-
RegexStringValidatorAttribute
-
StringValidatorAttribute
-
TimeSpanValidatorAttribute
-
-

ConfigurationCollectionAttribute を使用する方法の例を次に示します。
この例には 4 つのクラスが含まれています。TestingConfigurationCollectionAttribute クラスは、要素のコレクションを格納するカスタムの構成セクションを作成します。残りの 3 つのクラスは、カスタム セクションを作成するために使用されます。カスタム セクションの UrlsSection 型は、Urls プロパティを格納します。このプロパティは、UrlConfigElement 型のカスタム要素を格納する UrlsCollection 型のカスタム コレクションです。この例を動作させるために重要なのは、ConfigurationCollectionAttribute で装飾される Urls プロパティです。
Imports System Imports System.Configuration ' Define a property section named <urls> ' containing a UrlsCollection collection of ' UrlConfigElement elements. ' This section requires the definition of UrlsCollection and ' UrlsConfigElement types. Public Class UrlsSection Inherits ConfigurationSection ' Declare the collection element. Private url As UrlConfigElement Public Sub New() ' Create a collection element. ' The property values assigned to ' this instance are provided ' by the ConfigurationProperty attributes ' associated wiht the UrlConfigElement ' properties. url = New UrlConfigElement() End Sub 'New ' Declare the urls collection property. ' Note: the "IsDefaultCollection = false" instructs '.NET Framework to build a nested section of 'the kind <urls> ...</urls>. <ConfigurationProperty("urls", _ IsDefaultCollection:=False), _ ConfigurationCollection(GetType(UrlsCollection), _ AddItemName:="addUrl", _ ClearItemsName:="clearUrls", _ RemoveItemName:="RemoveUrl")> _ Public ReadOnly Property Urls() As UrlsCollection Get Dim urlCollection As UrlsCollection = _ CType(MyBase.Item("urls"), UrlsCollection) Return urlCollection End Get End Property End Class 'UrlsSection ' Define the UrlsCollection that will contain the ' UrlsConfigElement elements. Public Class UrlsCollection Inherits ConfigurationElementCollection Public Sub New() Dim url As UrlConfigElement = _ CType(CreateNewElement(), UrlConfigElement) Add(url) End Sub 'New Public Overrides ReadOnly Property CollectionType() _ As ConfigurationElementCollectionType Get Return ConfigurationElementCollectionType.AddRemoveClearMap End Get End Property Protected Overrides Function CreateNewElement() _ As ConfigurationElement Return New UrlConfigElement() End Function 'CreateNewElement Protected Overrides Function GetElementKey(ByVal element _ As ConfigurationElement) As [Object] Return CType(element, UrlConfigElement).Name End Function 'GetElementKey Default Public Shadows Property Item( _ ByVal index As Integer) As UrlConfigElement Get Return CType(BaseGet(index), UrlConfigElement) End Get Set(ByVal value As UrlConfigElement) If Not (BaseGet(index) Is Nothing) Then BaseRemoveAt(index) End If BaseAdd(index, value) End Set End Property Default Public Shadows ReadOnly Property Item( _ ByVal Name As String) As UrlConfigElement Get Return CType(BaseGet(Name), UrlConfigElement) End Get End Property Public Function IndexOf(ByVal url _ As UrlConfigElement) As Integer Return BaseIndexOf(url) End Function 'IndexOf Public Sub Add(url As UrlConfigElement) BaseAdd(url) End Sub 'Add Protected Overrides Sub BaseAdd(ByVal element _ As ConfigurationElement) BaseAdd(element, False) End Sub 'BaseAdd Public Overloads Sub Remove(ByVal url _ As UrlConfigElement) If BaseIndexOf(url) >= 0 Then BaseRemove(url.Name) End If End Sub 'Remove Public Sub RemoveAt(index As Integer) BaseRemoveAt(index) End Sub 'RemoveAt Overloads Public Sub Remove(name As String) BaseRemove(name) End Sub 'Remove Public Sub Clear() BaseClear() End Sub 'Clear End Class 'UrlsCollection ' Define the UrlConfigElement for the ' types contained by the UrlsSection. Public Class UrlConfigElement Inherits ConfigurationElement Public Sub New(name As String, url As String) Me.Name = name Me.Url = url End Sub 'New Public Sub New() End Sub <ConfigurationProperty("name", _ DefaultValue:="Microsoft", _ 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.microsoft.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(Me("port")) End Get Set(ByVal value As Integer) Me("port") = value End Set End Property Class TestingConfigurationCollectionAttribute Shared Sub ShowUrls() Try Dim myUrlsSection As UrlsSection = _ ConfigurationManager.GetSection("MyUrls") If myUrlsSection Is Nothing Then Console.WriteLine("Failed to load UrlsSection.") Else Console.WriteLine("My URLs:") Dim i As Integer For i = 0 To myUrlsSection.Urls.Count - 1 Console.WriteLine(" #{0} {1}: {2}", i, _ myUrlsSection.Urls(i).Name, _ myUrlsSection.Urls(i).Url + " port " + _ myUrlsSection.Urls(i).Port.ToString()) Next i End If Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'ShowUrls ' Create the custom section. ' It will contain a nested section as ' defined by the UrlsSection (<urls>...</urls>). Shared Sub CreateSection(sectionName As String) ' Get the current configuration (file). Dim config As System.Configuration.Configuration = _ ConfigurationManager.OpenExeConfiguration( _ ConfigurationUserLevel.None) Dim urlsSection As UrlsSection ' Create an entry in the <configSections>. If config.Sections(sectionName) Is Nothing Then urlsSection = New UrlsSection() config.Sections.Add(sectionName, urlsSection) config.Save() End If ' Create the actual target section and write it to ' the configuration file. If config.Sections(("/configuration/" + sectionName)) _ Is Nothing Then urlsSection = config.GetSection(sectionName) ' urlsSection.SectionInformation.ForceSave = True config.Save(ConfigurationSaveMode.Full) End If End Sub 'CreateSection Public Overloads Shared Sub Main(ByVal args() As String) Console.WriteLine("[Current URLs]") CreateSection("MyUrls") ShowUrls() Console.ReadLine() End Sub 'Main End Class 'TestingConfigurationCollectionAttribute
using System; using System.Configuration; namespace Samples.AspNet { // Define a property section named <urls> // containing a UrlsCollection collection of // UrlConfigElement elements. // This section requires the definition of UrlsCollection and // UrlsConfigElement types. public class UrlsSection : ConfigurationSection { // Declare the collection element. UrlConfigElement url; public UrlsSection() { // Create a collection element. // The property values assigned to // this instance are provided // by the ConfigurationProperty attributes // associated wiht the UrlConfigElement // properties. url = new UrlConfigElement(); } // Declare the urls collection property. // Note: the "IsDefaultCollection = false" instructs // .NET Framework to build a nested section of // the kind <urls> ...</urls>. [ConfigurationProperty("urls", IsDefaultCollection = false)] [ConfigurationCollection(typeof(UrlsCollection), AddItemName="addUrl", ClearItemsName="clearUrls", RemoveItemName="RemoveUrl")] public UrlsCollection Urls { get { UrlsCollection urlsCollection = (UrlsCollection)base["urls"]; return urlsCollection; } } } // Define the UrlsCollection that will contain the UrlsConfigElement // elements. public class UrlsCollection : ConfigurationElementCollection { public UrlsCollection() { UrlConfigElement url = (UrlConfigElement)CreateNewElement(); Add(url); } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.AddRemoveClearMap; } } protected override ConfigurationElement CreateNewElement() { return new UrlConfigElement(); } protected override Object GetElementKey(ConfigurationElement element) { return ((UrlConfigElement)element).Name; } public UrlConfigElement this[int index] { get { return (UrlConfigElement)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } new public UrlConfigElement this[string Name] { get { return (UrlConfigElement)BaseGet(Name); } } public int IndexOf(UrlConfigElement url) { return BaseIndexOf(url); } public void Add(UrlConfigElement url) { BaseAdd(url); } protected override void BaseAdd(ConfigurationElement element) { BaseAdd(element, false); } public void Remove(UrlConfigElement url) { if (BaseIndexOf(url) >= 0) BaseRemove(url.Name); } public void RemoveAt(int index) { BaseRemoveAt(index); } public void Remove(string name) { BaseRemove(name); } public void Clear() { BaseClear(); } } // Define the UrlConfigElement for the types contained by the // UrlsSection. public class UrlConfigElement : ConfigurationElement { public UrlConfigElement(String name, String url) { this.Name = name; this.Url = url; } public UrlConfigElement() { // Initialize as follows, if no attributed // values are provided. // this.Name = "Microsoft"; // this.Url = "http://www.microsoft.com"; // this.Port = 0; } [ConfigurationProperty("name", DefaultValue = "Microsoft" , IsRequired = true, IsKey = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("url", DefaultValue = "http://www.microsoft.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; } } } class TestingConfigurationCollectionAttribute { static void ShowUrls() { try { UrlsSection myUrlsSection = ConfigurationManager.GetSection("MyUrls") as UrlsSection; if (myUrlsSection == null) Console.WriteLine("Failed to load UrlsSection."); else { Console.WriteLine("My URLs:"); for (int i = 0; i < myUrlsSection.Urls.Count; i++) { Console.WriteLine(" #{0} {1}: {2}", i, myUrlsSection.Urls[i].Name, myUrlsSection.Urls[i].Url + " port " + myUrlsSection.Urls[i].Port); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } } // Create a custom section. // It will contain a nested section as // deined by the UrlsSection (<urls>...</urls>). static void CreateSection(string sectionName) { // Get the current configuration (file). System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); UrlsSection urlsSection; // Create an entry in the <configSections>. if (config.Sections[sectionName] == null) { urlsSection = new UrlsSection(); config.Sections.Add(sectionName, urlsSection); config.Save(); } // Create the actual target section and write it to // the configuration file. if (config.Sections["/configuration/" + sectionName] == null) { urlsSection = config.GetSection(sectionName) as UrlsSection; urlsSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } static void Main(string[] args) { Console.WriteLine("[Current URLs]"); CreateSection("MyUrls"); ShowUrls(); Console.ReadLine(); } } }
以下は、前の例で定義されていたカスタム セクションが含まれる構成ファイルからの抜粋です。
<configuration> <configSections> <section name="MyUrls" type="Samples.AspNet.UrlsSection, ConfigurationCollectionAttribute, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </configSections> <MyUrls> <urls> <clear /> <add name="Microsoft" url="http://www.microsoft.com" port="0" /> </urls> </MyUrls> </configuration>

System.Attribute
System.Configuration.ConfigurationCollectionAttribute


Windows 98, Windows 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ConfigurationCollectionAttribute メンバ
System.Configuration 名前空間
ConfigurationPropertyAttribute
IntegerValidatorAttribute
LongValidatorAttribute
RegexStringValidatorAttribute
StringValidatorAttribute
TimeSpanValidatorAttribute
ConfigurationCollectionAttribute コンストラクタ
アセンブリ: System.Configuration (system.configuration.dll 内)



ConfigurationCollectionAttribute を使用する方法の例を次に示します。
' Declare the urls collection property. ' Note: the "IsDefaultCollection = false" instructs '.NET Framework to build a nested section of 'the kind <urls> ...</urls>. <ConfigurationProperty("urls", _ IsDefaultCollection:=False), _ ConfigurationCollection(GetType(UrlsCollection), _ AddItemName:="addUrl", _ ClearItemsName:="clearUrls", _ RemoveItemName:="RemoveUrl")> _ Public ReadOnly Property Urls() As UrlsCollection Get Dim urlCollection As UrlsCollection = _ CType(MyBase.Item("urls"), UrlsCollection) Return urlCollection End Get End Property
// Declare the urls collection property. // Note: the "IsDefaultCollection = false" instructs // .NET Framework to build a nested section of // the kind <urls> ...</urls>. [ConfigurationProperty("urls", IsDefaultCollection = false)] [ConfigurationCollection(typeof(UrlsCollection), AddItemName="addUrl", ClearItemsName="clearUrls", RemoveItemName="RemoveUrl")] public UrlsCollection Urls { get { UrlsCollection urlsCollection = (UrlsCollection)base["urls"]; return urlsCollection; } }

Windows 98, Windows 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ConfigurationCollectionAttribute クラス
ConfigurationCollectionAttribute メンバ
System.Configuration 名前空間
ConfigurationCollectionAttribute プロパティ

名前 | 説明 | |
---|---|---|
![]() | AddItemName | add 構成要素の名前を取得または設定します。 |
![]() | ClearItemsName | clear 構成要素の名前を取得または設定します。 |
![]() | CollectionType | ConfigurationCollectionAttribute の種類を取得または設定します。 |
![]() | ItemType | コレクション要素の型を取得します。 |
![]() | RemoveItemName | remove 構成要素の名前を取得または設定します。 |
![]() | TypeId | 派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。 ( Attribute から継承されます。) |

関連項目
ConfigurationCollectionAttribute クラスSystem.Configuration 名前空間
ConfigurationPropertyAttribute
IntegerValidatorAttribute
LongValidatorAttribute
RegexStringValidatorAttribute
StringValidatorAttribute
TimeSpanValidatorAttribute
ConfigurationCollectionAttribute メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

関連項目
ConfigurationCollectionAttribute クラスSystem.Configuration 名前空間
ConfigurationPropertyAttribute
IntegerValidatorAttribute
LongValidatorAttribute
RegexStringValidatorAttribute
StringValidatorAttribute
TimeSpanValidatorAttribute
ConfigurationCollectionAttribute メンバ
.NET Framework が構成要素のコレクションをインスタンス化するように、宣言によって指示します。このクラスは継承できません。
ConfigurationCollectionAttribute データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | AddItemName | add 構成要素の名前を取得または設定します。 |
![]() | ClearItemsName | clear 構成要素の名前を取得または設定します。 |
![]() | CollectionType | ConfigurationCollectionAttribute の種類を取得または設定します。 |
![]() | ItemType | コレクション要素の型を取得します。 |
![]() | RemoveItemName | remove 構成要素の名前を取得または設定します。 |
![]() | TypeId | 派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。(Attribute から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

関連項目
ConfigurationCollectionAttribute クラスSystem.Configuration 名前空間
ConfigurationPropertyAttribute
IntegerValidatorAttribute
LongValidatorAttribute
RegexStringValidatorAttribute
StringValidatorAttribute
TimeSpanValidatorAttribute
- ConfigurationCollectionAttributeのページへのリンク