Attribute クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
<SerializableAttribute> _ <ClassInterfaceAttribute(ClassInterfaceType.None)> _ <ComVisibleAttribute(True)> _ <AttributeUsageAttribute(AttributeTargets.All, Inherited:=True, AllowMultiple:=False)> _ Public MustInherit Class Attribute Implements _Attribute
[SerializableAttribute] [ClassInterfaceAttribute(ClassInterfaceType.None)] [ComVisibleAttribute(true)] [AttributeUsageAttribute(AttributeTargets.All, Inherited=true, AllowMultiple=false)] public abstract class Attribute : _Attribute
[SerializableAttribute] [ClassInterfaceAttribute(ClassInterfaceType::None)] [ComVisibleAttribute(true)] [AttributeUsageAttribute(AttributeTargets::All, Inherited=true, AllowMultiple=false)] public ref class Attribute abstract : _Attribute
/** @attribute SerializableAttribute() */ /** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ /** @attribute ComVisibleAttribute(true) */ /** @attribute AttributeUsageAttribute(AttributeTargets.All, Inherited=true, AllowMultiple=false) */ public abstract class Attribute implements _Attribute
Attribute クラスは、定義済みのシステム情報またはユーザー定義のカスタム情報をターゲット要素に関連付けます。ターゲット要素には、アセンブリ、クラス、コンストラクタ、デリゲート、列挙型、イベント、フィールド、インターフェイス、メソッド、移植可能な実行可能 (PE) ファイル モジュール、パラメータ、プロパティ、戻り値、構造体、または他の属性を指定できます。
属性が提供する情報は、メタデータとも呼ばれます。メタデータは、プログラムでデータをどのように処理するかを制御するために、アプリケーションによって実行時にチェックされる場合もあれば、アプリケーション自体をどのように処理または維持するかを制御するために、外部ツールによって実行時前にチェックされる場合もあります。たとえば、.NET Framework では、実行時の動作を制御するために、属性型を事前定義して使用します。また、一部のプログラミング言語では、属性型を使用して、.NET Framework の共通型システムで直接サポートされていない言語機能を表します。
すべての属性型は、Attribute クラスから直接または間接的に派生します。属性は、どのターゲット要素にでも適用できます。1 つのターゲット要素に複数の属性を適用できます。また、ターゲット要素から派生した要素に属性を継承することもできます。属性を適用するターゲット要素を指定するには、AttributeTargets クラスを使用します。
Attribute クラスには、カスタム属性を取得し、テストするための便利なメソッドが用意されています。属性の使用方法については、「属性を使用したメタデータの拡張」を参照してください。
Imports System Imports System.Reflection Public Module CustomAttrVB ' An enumeration of animals. Start at 1 (0 = uninitialized). Public Enum Animal ' Pets Dog = 1 Cat Bird End Enum ' Visual Basic requires the AttributeUsage be specified. ' A custom attribute to allow a target to have a pet. <AttributeUsage(AttributeTargets.Method)> _ Public Class AnimalTypeAttribute Inherits Attribute ' The constructor is called when the attribute is set. Public Sub New(ByVal animal As Animal) Me.thePet = animal End Sub ' Keep a variable internally ... Protected thePet As Animal ' .. and show a copy to the outside world. Public Property Pet() As Animal Get Return thePet End Get Set(ByVal Value As Animal) thePet = Value End Set End Property End Class ' A test class where each method has its own pet. Class AnimalTypeTestClass <AnimalType(Animal.Dog)> _ Public Sub DogMethod() End Sub <AnimalType(Animal.Cat)> _ Public Sub CatMethod() End Sub <AnimalType(Animal.Bird)> _ Public Sub BirdMethod() End Sub End Class ' The runtime test. Sub Main() Dim testClass As New AnimalTypeTestClass() Dim tcType As Type = testClass.GetType() Dim mInfo As MethodInfo ' Iterate through all the methods of the class. For Each mInfo In tcType.GetMethods() Dim attr As Attribute ' Iterate through all the attributes of the method. For Each attr In Attribute.GetCustomAttributes(mInfo) If TypeOf attr Is AnimalTypeAttribute Then Dim attrCustom As AnimalTypeAttribute = _ CType(attr, AnimalTypeAttribute) Console.WriteLine("Method {0} has a pet {1} attribute.", _ mInfo.Name(), attrCustom.Pet.ToString()) End If Next Next End Sub End Module ' Output: ' Method DogMethod has a pet Dog attribute. ' Method CatMethod has a pet Cat attribute. ' Method BirdMethod has a pet Bird attribute.
using System; using System.Reflection; namespace CustomAttrCS { // An enumeration of animals. Start at 1 (0 = uninitialized). public enum Animal { // Pets. Dog = 1, Cat, Bird, } // A custom attribute to allow a target to have a pet. public class AnimalTypeAttribute : Attribute { // The constructor is called when the attribute is set. public AnimalTypeAttribute(Animal pet) { thePet = pet; } // Keep a variable internally ... protected Animal thePet; // .. and show a copy to the outside world. public Animal Pet { get { return thePet; } set { thePet = Pet; } } } // A test class where each method has its own pet. class AnimalTypeTestClass { [AnimalType(Animal.Dog)] public void DogMethod() {} [AnimalType(Animal.Cat)] public void CatMethod() {} [AnimalType(Animal.Bird)] public void BirdMethod() {} } class DemoClass { static void Main(string[] args) { AnimalTypeTestClass testClass = new AnimalTypeTestClass(); Type type = testClass.GetType(); // Iterate through all the methods of the class. foreach(MethodInfo mInfo in type.GetMethods()) { // Iterate through all the Attributes for each method. foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo)) { // Check for the AnimalType attribute. if (attr.GetType() == typeof(AnimalTypeAttribute)) Console.WriteLine( "Method {0} has a pet {1} attribute.", mInfo.Name, ((AnimalTypeAttribute)attr).Pet); } } } } } /* * Output: * Method DogMethod has a pet Dog attribute. * Method CatMethod has a pet Cat attribute. * Method BirdMethod has a pet Bird attribute. */
using namespace System; using namespace System::Reflection; // An enumeration of animals. Start at 1 (0 = uninitialized). public enum class Animal { // Pets. Dog = 1, Cat, Bird }; // A custom attribute to allow a target to have a pet. public ref class AnimalTypeAttribute: public Attribute { public: // The constructor is called when the attribute is set. AnimalTypeAttribute( Animal pet ) { thePet = pet; } protected: // Keep a variable internally ... Animal thePet; public: property Animal Pet { // .. and show a copy to the outside world. Animal get() { return thePet; } void set( Animal value ) { thePet = value; } } }; // A test class where each method has its own pet. ref class AnimalTypeTestClass { public: [AnimalType(Animal::Dog)] void DogMethod(){} [AnimalType(Animal::Cat)] void CatMethod(){} [AnimalType(Animal::Bird)] void BirdMethod(){} }; int main() { AnimalTypeTestClass^ testClass = gcnew AnimalTypeTestClass; Type^ type = testClass->GetType(); // Iterate through all the methods of the class. System::Collections::IEnumerator^ myEnum = type->GetMethods()->GetEnumerator(); while ( myEnum->MoveNext() ) { MethodInfo^ mInfo = safe_cast<MethodInfo^>(myEnum->Current); // Iterate through all the Attributes for each method. System::Collections::IEnumerator^ myEnum1 = Attribute::GetCustomAttributes( mInfo )->GetEnumerator(); while ( myEnum1->MoveNext() ) { Attribute^ attr = safe_cast<Attribute^>(myEnum1->Current); // Check for the AnimalType attribute. if ( attr->GetType() == AnimalTypeAttribute::typeid ) Console::WriteLine( "Method {0} has a pet {1} attribute.", mInfo->Name, (dynamic_cast<AnimalTypeAttribute^>(attr))->Pet ); } } } /* * Output: * Method DogMethod has a pet Dog attribute. * Method CatMethod has a pet Cat attribute. * Method BirdMethod has a pet Bird attribute. */
package CustomAttrJSL; import System.*; import System.Reflection.*; public class Animal { protected int pet; public static final int dog = 1; public static final int cat = 2; public static final int bird = 3; public Animal() { pet = 0; } //Animal public Animal(int p) { pet = p; } //Animal public String getPet() { switch (pet) { case 1 : return "Dog"; case 2 : return "Cat"; case 3 : return "Bird"; default : return null; } } //getPet } //Animal // A custom attribute to allow a target to have a pet. /** @attribute System.AttributeUsageAttribute(AttributeTargets.Method) */ public class AnimalTypeAttribute extends Attribute { public AnimalTypeAttribute() { } // The constructor is called when the attribute is set. public AnimalTypeAttribute(int pet) { thePet = new Animal(pet); } //AnimalTypeAttribute // Keep a variable internally... protected Animal thePet; //.. and show a copy to the outside world. /** @property */ public Animal get_Pet() { return thePet; } //get_Pet /** @property */ public void set_Pet(Animal value) { thePet = value; } //set_Pet } //AnimalTypeAttribute // A test class where each method has its own pet. class AnimalTypeTestClass { /** @attribute AnimalType(Animal.dog) */ public void DogMethod() { } //DogMethod /** @attribute AnimalType(Animal.cat) */ public void CatMethod() { } //CatMethod /** @attribute AnimalType(Animal.bird) */ public void BirdMethod() { } //BirdMethod } //AnimalTypeTestClass class DemoClass { public static void main(String[] args) { AnimalTypeTestClass testClass = new AnimalTypeTestClass(); Type type = testClass.GetType(); // Iterate through all the methods of the class. for (int iCtr1 = 0; iCtr1 < type.GetMethods().length; iCtr1++) { MethodInfo mInfo = type.GetMethods()[iCtr1]; // Iterate through all the Attributes for each method. for (int iCtr2 = 0; iCtr2 < Attribute.GetCustomAttributes(mInfo).length; iCtr2++) { Attribute attr = Attribute.GetCustomAttributes(mInfo)[iCtr2]; // Check for the AnimalType attribute. if (attr.GetType(). Equals(AnimalTypeAttribute.class.ToType())) { Console.WriteLine("Method {0} has a pet {1} attribute.", mInfo.get_Name(), ((AnimalTypeAttribute)attr).get_Pet().getPet()); } } } } //main } //DemoClass /* Output: Method DogMethod has a pet Dog attribute. Method CatMethod has a pet Cat attribute. Method BirdMethod has a pet Bird attribute. */
import System; import System.Reflection; import CustomAttrJS; package CustomAttrJS { // An enumeration of animals. Start at 1 (0 = uninitialized). public enum Animal { // Pets. Dog = 1, Cat, Bird, } // A custom attribute to allow a target to have a pet. public AttributeUsage(AttributeTargets.Method) class AnimalTypeAttribute extends Attribute { // The constructor is called when the attribute is set. public function AnimalTypeAttribute(pet : Animal) { thePet = pet; } // Keep a variable internally ... protected var thePet : Animal; // .. and show a copy to the outside world. public function get Pet() : Animal { return thePet; } public function set Pet(pet : Animal) { thePet = pet; } } // A test class where each method has its own pet. class AnimalTypeTestClass { public AnimalTypeAttribute(Animal.Dog) function DogMethod() {} public AnimalTypeAttribute(Animal.Cat) function CatMethod() {} public AnimalTypeAttribute(Animal.Bird) function BirdMethod() {} } } var testClass : AnimalTypeTestClass = new AnimalTypeTestClass(); var type : Type = testClass.GetType(); // Iterate through all the methods of the class. var mInfo : MethodInfo[] = type.GetMethods(); for (var i : int = 0; i < mInfo.length; i++) { // Iterate through all the Attributes for each method. var attr : Attribute[] = Attribute.GetCustomAttributes(mInfo[i]); for (var j : int = 0; j < attr.length; j++) { // Check for the AnimalType attribute. if (attr[j].GetType() == AnimalTypeAttribute) Console.WriteLine( "Method {0} has a pet {1} attribute.", mInfo[i].Name, AnimalTypeAttribute(attr[j]).Pet); } } /* * Output: * Method BirdMethod has a pet Bird attribute. * Method CatMethod has a pet Cat attribute. * Method DogMethod has a pet Dog attribute. * */
スレッド セーフ
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Attribute コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)
構文
解説
使用例
パラメータのカスタム Attribute クラスとそのコンストラクタの定義を次のコード例に示します。
' Define a custom parameter attribute that takes a single message argument. <AttributeUsage(AttributeTargets.Parameter)> _ Public Class ArgumentUsageAttribute Inherits Attribute ' This is the attribute constructor. Public Sub New(UsageMsg As String) Me.usageMsg = UsageMsg End Sub ' New ' usageMsg is storage for the attribute message. Protected usageMsg As String ' This is the Message property for the attribute. Public Property Message() As String Get Return usageMsg End Get Set usageMsg = value End Set End Property End Class ' ArgumentUsageAttribute
// Define a custom parameter attribute that takes a single message argument. [AttributeUsage( AttributeTargets.Parameter )] public class ArgumentUsageAttribute : Attribute { // This is the attribute constructor. public ArgumentUsageAttribute( string UsageMsg ) { this.usageMsg = UsageMsg; } // usageMsg is storage for the attribute message. protected string usageMsg; // This is the Message property for the attribute. public string Message { get { return usageMsg; } set { usageMsg = value; } } }
// Define a custom parameter attribute that takes a single message argument. [AttributeUsage(AttributeTargets::Parameter)] public ref class ArgumentUsageAttribute: public Attribute { protected: // usageMsg is storage for the attribute message. String^ usageMsg; public: // This is the attribute constructor. ArgumentUsageAttribute( String^ UsageMsg ) { this->usageMsg = UsageMsg; } property String^ Message { // This is the Message property for the attribute. String^ get() { return usageMsg; } void set( String^ value ) { this->usageMsg = value; } } };
// Define a custom parameter attribute that takes a single message argument. /** @attribute AttributeUsage(AttributeTargets.Parameter) */ public class ArgumentUsageAttribute extends Attribute { // This is the attribute constructor. public ArgumentUsageAttribute(String usgMsg) { this.usageMsg = usgMsg; } //ArgumentUsageAttribute // usageMsg is storage for the attribute message. protected String usageMsg; // This is the Message property for the attribute. /** @property */ public String get_Message() { return usageMsg; } //get_Message /** @property */ public void set_Message(String value) { usageMsg = value; } //set_Message } //ArgumentUsageAttribute
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Attribute プロパティ
Attribute メソッド
パブリック メソッド
名前 | 説明 | |
---|---|---|
Equals | オーバーロードされます。 オーバーライドされます。 | |
GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 | |
GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 | |
GetHashCode | オーバーライドされます。 このインスタンスのハッシュ コードを返します。 | |
GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) | |
IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 | |
IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 | |
Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) | |
ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
名前 | 説明 | |
---|---|---|
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
名前 | 説明 | |
---|---|---|
System.Runtime.InteropServices._Attribute.GetIDsOfNames | 一連の名前を対応する一連のディスパッチ識別子に割り当てます。 | |
System.Runtime.InteropServices._Attribute.GetTypeInfo | オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。 | |
System.Runtime.InteropServices._Attribute.GetTypeInfoCount | オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 | |
System.Runtime.InteropServices._Attribute.Invoke | オブジェクトが公開するプロパティおよびメソッドにアクセスできるようにします。 |
Attribute メンバ
Attribute データ型で公開されるメンバを以下の表に示します。
プロテクト コンストラクタ
パブリック プロパティ
パブリック メソッド
名前 | 説明 | |
---|---|---|
Equals | オーバーロードされます。 オーバーライドされます。 | |
GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 | |
GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 | |
GetHashCode | オーバーライドされます。 このインスタンスのハッシュ コードを返します。 | |
GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) | |
IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 | |
IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 | |
Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) | |
ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
名前 | 説明 | |
---|---|---|
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
名前 | 説明 | |
---|---|---|
System.Runtime.InteropServices._Attribute.GetIDsOfNames | 一連の名前を対応する一連のディスパッチ識別子に割り当てます。 | |
System.Runtime.InteropServices._Attribute.GetTypeInfo | オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。 | |
System.Runtime.InteropServices._Attribute.GetTypeInfoCount | オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 | |
System.Runtime.InteropServices._Attribute.Invoke | オブジェクトが公開するプロパティおよびメソッドにアクセスできるようにします。 |
_Attribute インターフェイス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
<ComVisibleAttribute(True)> _ <GuidAttribute("917B14D0-2D9E-38B8-92A9-381ACF52F7C0")> _ <InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _ <CLSCompliantAttribute(False)> _ Public Interface _Attribute
[ComVisibleAttribute(true)] [GuidAttribute("917B14D0-2D9E-38B8-92A9-381ACF52F7C0")] [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] [CLSCompliantAttribute(false)] public interface _Attribute
[ComVisibleAttribute(true)] [GuidAttribute(L"917B14D0-2D9E-38B8-92A9-381ACF52F7C0")] [InterfaceTypeAttribute(ComInterfaceType::InterfaceIsIUnknown)] [CLSCompliantAttribute(false)] public interface class _Attribute
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
_Attribute メソッド
パブリック メソッド
名前 | 説明 | |
---|---|---|
GetIDsOfNames | 一連の名前を対応する一連のディスパッチ識別子に割り当てます。 | |
GetTypeInfo | オブジェクトの型情報を取得します。その後は、インターフェイスの型情報の取得に使用できます。 | |
GetTypeInfoCount | オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 | |
Invoke | オブジェクトが公開するプロパティおよびメソッドにアクセスできるようにします。 |
_Attribute メンバ
System.Attribute クラスをアンマネージ コードに公開します。
_Attribute データ型で公開されるメンバを以下の表に示します。
パブリック メソッド
名前 | 説明 | |
---|---|---|
GetIDsOfNames | 一連の名前を対応する一連のディスパッチ識別子に割り当てます。 | |
GetTypeInfo | オブジェクトの型情報を取得します。その後は、インターフェイスの型情報の取得に使用できます。 | |
GetTypeInfoCount | オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 | |
Invoke | オブジェクトが公開するプロパティおよびメソッドにアクセスできるようにします。 |
Weblioに収録されているすべての辞書からattributeを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からattributeを検索
- attributeのページへのリンク