Attribute 類別

定義

代表自訂屬性的基底類別。

public ref class Attribute abstract
public ref class Attribute abstract : System::Runtime::InteropServices::_Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
public abstract class Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
public abstract class Attribute : System.Runtime.InteropServices._Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Attribute : System.Runtime.InteropServices._Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
type Attribute = class
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
type Attribute = class
    interface _Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Attribute = class
    interface _Attribute
Public MustInherit Class Attribute
Public MustInherit Class Attribute
Implements _Attribute
繼承
Attribute
衍生
屬性
實作

範例

下列程式代碼範例示範的使用方式 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.
 */
using System;
using System.Reflection;

// 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 = value; }
    }
}

// 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.
 */
open System

// An enumeration of animals. Start at 1 (0 = uninitialized).
type Animal =
    | Dog = 1
    | Cat = 2
    | Bird = 3

// A custom attribute to allow a target to have a pet.
type AnimalTypeAttribute(pet) =
    inherit Attribute()
    member val Pet = pet with get, set

// A test class where each method has its own pet.
type AnimalTypeTestClass() =
    [<AnimalType(Animal.Dog)>]
    member _.DogMethod() = ()

    [<AnimalType(Animal.Cat)>]
    member _.CatMethod() = ()

    [<AnimalType(Animal.Bird)>]
    member _.BirdMethod() = ()

let testClass = AnimalTypeTestClass()
let clsType = testClass.GetType()
// Iterate through all the methods of the class.
for mInfo in clsType.GetMethods() do
    // Iterate through all the Attributes for each method.
    for attr in Attribute.GetCustomAttributes mInfo do
        // Check for the AnimalType attribute.
        if attr.GetType() = typeof<AnimalTypeAttribute> then
            printfn $"Method {mInfo.Name} has a pet {(attr :?> AnimalTypeAttribute).Pet} attribute."

// Output:
//   Method DogMethod has a pet Dog attribute.
//   Method CatMethod has a pet Cat attribute.
//   Method BirdMethod has a pet Bird attribute.
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.

備註

類別 Attribute 會將預先定義的系統資訊或使用者定義的自定義資訊與目標專案產生關聯。 目標專案可以是元件、類別、建構函式、委派、列舉、事件、欄位、介面、方法、可攜式可執行檔模組、參數、屬性、傳回值、結構或其他屬性。

屬性提供的資訊也稱為元數據。 應用程式可以在運行時間檢查元數據,以控制程式處理數據的方式,或在外部工具運行時間之前檢查元數據,以控制應用程式本身的處理或維護方式。 例如,.NET 預先定義並使用屬性類型來控制運行時間行為,而某些程式設計語言會使用屬性類型來代表 .NET 通用類型系統未直接支援的語言功能。

所有屬性類型都會直接或間接衍生自 Attribute 類別。 屬性可以套用至任何目標專案;多個屬性可以套用至相同的目標專案;和屬性可由衍生自目標元素的項目繼承。 AttributeTargets使用類別來指定套用屬性的目標專案。

類別 Attribute 提供方便的方法,可擷取及測試自定義屬性。 如需使用屬性的詳細資訊,請參閱 套用屬性屬性

建構函式

Attribute()

初始化 Attribute 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

GetCustomAttribute(Assembly, Type)

擷取套用至指定之組件的自訂屬性。 參數會指定組件,以及要搜尋的自訂屬性型別。

GetCustomAttribute(Assembly, Type, Boolean)

擷取套用至組件的自訂屬性。 參數會指定組件、要搜尋的自訂屬性型別,以及忽略的搜尋選項。

GetCustomAttribute(MemberInfo, Type)

擷取套用至型別成員的自訂屬性。 參數會指定成員,以及要搜尋的自訂屬性型別。

GetCustomAttribute(MemberInfo, Type, Boolean)

擷取套用至型別成員的自訂屬性。 參數會指定成員、要搜尋的自訂屬性型別,以及是否要搜尋成員的祖系。

GetCustomAttribute(Module, Type)

擷取套用至模組的自訂屬性。 參數會指定模組,以及要搜尋的自訂屬性型別。

GetCustomAttribute(Module, Type, Boolean)

擷取套用至模組的自訂屬性。 參數會指定模組、要搜尋的自訂屬性型別,以及忽略的搜尋選項。

GetCustomAttribute(ParameterInfo, Type)

擷取套用至方法參數的自訂屬性。 參數會指定方法參數,以及要搜尋的自訂屬性型別。

GetCustomAttribute(ParameterInfo, Type, Boolean)

擷取套用至方法參數的自訂屬性。 參數會指定方法參數、要搜尋的自訂屬性型別,以及是否要搜尋方法參數的祖系。

GetCustomAttributes(Assembly)

擷取套用至組件的自訂屬性陣列。 參數會指定組件。

GetCustomAttributes(Assembly, Boolean)

擷取套用至組件的自訂屬性陣列。 參數會指定組件和忽略的搜尋選項。

GetCustomAttributes(Assembly, Type)

擷取套用至組件的自訂屬性陣列。 參數會指定組件,以及要搜尋的自訂屬性型別。

GetCustomAttributes(Assembly, Type, Boolean)

擷取套用至組件的自訂屬性陣列。 參數會指定組件、要搜尋的自訂屬性型別,以及忽略的搜尋選項。

GetCustomAttributes(MemberInfo)

擷取套用至型別成員的自訂屬性陣列。 參數會指定成員。

GetCustomAttributes(MemberInfo, Boolean)

擷取套用至型別成員的自訂屬性陣列。 參數會指定成員、要搜尋的自訂屬性型別,以及是否要搜尋成員的祖系。

GetCustomAttributes(MemberInfo, Type)

擷取套用至型別成員的自訂屬性陣列。 參數會指定成員,以及要搜尋的自訂屬性型別。

GetCustomAttributes(MemberInfo, Type, Boolean)

擷取套用至型別成員的自訂屬性陣列。 參數會指定成員、要搜尋的自訂屬性型別,以及是否要搜尋成員的祖系。

GetCustomAttributes(Module)

擷取套用至模組的自訂屬性陣列。 參數會指定模組。

GetCustomAttributes(Module, Boolean)

擷取套用至模組的自訂屬性陣列。 參數會指定模組和忽略的搜尋選項。

GetCustomAttributes(Module, Type)

擷取套用至模組的自訂屬性陣列。 參數會指定模組,以及要搜尋的自訂屬性型別。

GetCustomAttributes(Module, Type, Boolean)

擷取套用至模組的自訂屬性陣列。 參數會指定模組、要搜尋的自訂屬性型別,以及忽略的搜尋選項。

GetCustomAttributes(ParameterInfo)

擷取套用至方法參數的自訂屬性陣列。 參數會指定方法參數。

GetCustomAttributes(ParameterInfo, Boolean)

擷取套用至方法參數的自訂屬性陣列。 參數會指定方法參數,以及是否要搜尋方法參數的祖系。

GetCustomAttributes(ParameterInfo, Type)

擷取套用至方法參數的自訂屬性陣列。 參數會指定方法參數,以及要搜尋的自訂屬性型別。

GetCustomAttributes(ParameterInfo, Type, Boolean)

擷取套用至方法參數的自訂屬性陣列。 參數會指定方法參數、要搜尋的自訂屬性型別,以及是否要搜尋方法參數的祖系。

GetHashCode()

傳回這個執行個體的雜湊碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

IsDefined(Assembly, Type)

判斷是否將任何自訂屬性套用至組件。 參數會指定組件,以及要搜尋的自訂屬性型別。

IsDefined(Assembly, Type, Boolean)

判斷是否將任何自訂屬性套用至組件。 參數會指定組件、要搜尋的自訂屬性型別,以及忽略的搜尋選項。

IsDefined(MemberInfo, Type)

判斷是否將任何自訂屬性套用至型別成員。 參數會指定成員,以及要搜尋的自訂屬性型別。

IsDefined(MemberInfo, Type, Boolean)

判斷是否將任何自訂屬性套用至型別成員。 參數會指定成員、要搜尋的自訂屬性型別,以及是否要搜尋成員的祖系。

IsDefined(Module, Type)

判斷是否將任何指定型別的自訂屬性套用至模組。 參數會指定模組,以及要搜尋的自訂屬性型別。

IsDefined(Module, Type, Boolean)

判斷是否將任何自訂屬性套用至模組。 參數會指定模組、要搜尋的自訂屬性型別,以及忽略的搜尋選項。

IsDefined(ParameterInfo, Type)

判斷是否將任何自訂屬性套用至方法參數。 參數會指定方法參數,以及要搜尋的自訂屬性型別。

IsDefined(ParameterInfo, Type, Boolean)

判斷是否將任何自訂屬性套用至方法參數。 參數會指定方法參數、要搜尋的自訂屬性型別,以及是否要搜尋方法參數的祖系。

Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

適用於

執行緒安全性

此型別具備執行緒安全。

另請參閱