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)

이름 집합을 해당하는 디스패치 식별자 집합에 매핑합니다.

_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

_Attribute.GetTypeInfoCount(UInt32)

개체에서 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

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

개체에서 노출하는 메서드와 속성에 대한 액세스를 제공합니다.

적용 대상

스레드 보안

이 형식은 스레드로부터 안전합니다.

추가 정보