Attribute クラス
カスタム属性の基本クラスです。
この型のすべてのメンバの一覧については、Attribute メンバ を参照してください。
System.Object
System.Attribute
派生クラス
<AttributeUsage(AttributeTargets.All)>
<Serializable>
MustInherit Public Class Attribute
[C#]
[AttributeUsage(AttributeTargets.All)]
[Serializable]
public abstract class Attribute
[C++]
[AttributeUsage(AttributeTargets::All)]
[Serializable]
public __gc __abstract class Attribute
[JScript]
public
AttributeUsage(AttributeTargets.All)
Serializable
abstract class Attribute
スレッドセーフ
この型は、マルチスレッド操作に対して安全です。
解説
Attribute クラスには、カスタム属性へアクセスしてこの属性をテストするための便利なメソッドが含まれています。すべてのユーザー定義型を属性として使用できますが、ほとんどの属性は、 Attribute から派生した型のインスタンスであると予想されます。
属性はすべて、 Attribute から直接的または間接的に派生します。属性は、すべてのターゲット要素に適用できます。詳細については、 AttributeTargets のトピックを参照してください。1 つの属性の複数のインスタンスを同一ターゲット要素へ適用できます。また、ターゲット要素から派生した要素へ属性を継承することもできます。コンパイラやその他の開発ツールでは、この情報を使用してカスタム属性が識別されます。
カスタム属性をメタデータの任意の要素と共に格納できます。この機構を利用して、コンパイル時にアプリケーション固有の情報を格納し、実行時または他のツールによるメタデータの読み取り時にこの情報へアクセスできます。
.NET Framework では一部の属性の型が定義されており、これらの型によって実行時の動作が制御されます。一部の言語では、.NET Framework の共通型システム (CTS: Common Type System) では直接表されない言語の機能を表す属性の型が定義されています。ユーザーまたはその他のツールが、属性の型を新規に定義して使用できます。
属性の使用方法については、「 属性を使用したメタデータの拡張 」を参照してください。
使用例
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.
[C#]
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.
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
// An enumeration of animals. Start at 1 (0 = uninitialized).
public __value enum Animal {
// Pets.
Dog = 1,
Cat,
Bird,
};
// A custom attribute to allow a target to have a pet.
public __gc class AnimalTypeAttribute : public 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:
__property Animal get_Pet() { return thePet; }
__property void set_Pet( Animal value ) { thePet = value; }
};
// A test class where each method has its own pet.
__gc class AnimalTypeTestClass {
public:
[AnimalType(Animal::Dog)]
void DogMethod() {}
[AnimalType(Animal::Cat)]
void CatMethod() {}
[AnimalType(Animal::Bird)]
void BirdMethod() {}
};
int main() {
AnimalTypeTestClass* testClass = new 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 = __try_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 = __try_cast<Attribute*>(myEnum1->Current);
// Check for the AnimalType attribute.
if (attr->GetType() == __typeof(AnimalTypeAttribute))
Console::WriteLine(
S"Method {0} has a pet {1} attribute.",
mInfo->Name, __box((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.
*/
[JScript]
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.
*/
必要条件
名前空間: System
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
アセンブリ: Mscorlib (Mscorlib.dll 内)