AttributeTargets 열거형
정의
특성을 적용하는 데 유효한 애플리케이션 요소를 지정합니다.Specifies the application elements on which it is valid to apply an attribute.
이 열거형에는 멤버 값의 비트 조합을 허용하는 FlagsAttribute 특성이 있습니다.
public enum class AttributeTargets
public enum AttributeTargets
[System.Flags]
public enum AttributeTargets
[System.Flags]
[System.Serializable]
public enum AttributeTargets
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum AttributeTargets
type AttributeTargets =
[<System.Flags>]
type AttributeTargets =
[<System.Flags>]
[<System.Serializable>]
type AttributeTargets =
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type AttributeTargets =
Public Enum AttributeTargets
- 상속
- 특성
필드
All | 32767 | 특성은 모든 애플리케이션 요소에 적용할 수 있습니다.Attribute can be applied to any application element. |
Assembly | 1 | 특성은 어셈블리에 적용할 수 있습니다.Attribute can be applied to an assembly. |
Class | 4 | 특성은 클래스에 적용할 수 있습니다.Attribute can be applied to a class. |
Constructor | 32 | 특성은 생성자에 적용할 수 있습니다.Attribute can be applied to a constructor. |
Delegate | 4096 | 특성은 대리자에 적용할 수 있습니다.Attribute can be applied to a delegate. |
Enum | 16 | 특성은 열거형에 적용할 수 있습니다.Attribute can be applied to an enumeration. |
Event | 512 | 특성은 이벤트에 적용할 수 있습니다.Attribute can be applied to an event. |
Field | 256 | 특성은 필드에 적용할 수 있습니다.Attribute can be applied to a field. |
GenericParameter | 16384 | 특성은 제네릭 매개 변수에 적용할 수 있습니다.Attribute can be applied to a generic parameter. 현재 이 특성은 C#, MSIL(Microsoft Intermediate language) 및 내보낸 코드에만 적용할 수 있습니다.Currently, this attribute can be applied only in C#, Microsoft intermediate language (MSIL), and emitted code. |
Interface | 1024 | 특성은 인터페이스에 적용할 수 있습니다.Attribute can be applied to an interface. |
Method | 64 | 특성은 메서드에 적용할 수 있습니다.Attribute can be applied to a method. |
Module | 2 | 특성은 모듈에 적용할 수 있습니다.Attribute can be applied to a module. |
Parameter | 2048 | 특성은 매개 변수에 적용할 수 있습니다.Attribute can be applied to a parameter. |
Property | 128 | 특성은 속성에 적용할 수 있습니다.Attribute can be applied to a property. |
ReturnValue | 8192 | 특성은 반환 값에 적용할 수 있습니다.Attribute can be applied to a return value. |
Struct | 8 | 특성은 구조체 즉, 값 형식에 적용할 수 있습니다.Attribute can be applied to a structure; that is, a value type. |
예제
다음 예에서는 다양 한 대상에 특성을 적용 하는 방법을 보여 줍니다.The following example illustrates the application of attributes to various targets.
참고
Visual Basic 및 Visual c + + 구문에 현재 형식 매개 변수 특성의 애플리케이션을 지원 하지 않습니다.Visual Basic and Visual C++ syntax currently do not support the application of attributes to type parameters.
using namespace System;
namespace AttTargsCS
{
// This attribute is only valid on a class.
[AttributeUsage(AttributeTargets::Class)]
public ref class ClassTargetAttribute: public Attribute{};
// This attribute is only valid on a method.
[AttributeUsage(AttributeTargets::Method)]
public ref class MethodTargetAttribute: public Attribute{};
// This attribute is only valid on a constructor.
[AttributeUsage(AttributeTargets::Constructor)]
public ref class ConstructorTargetAttribute: public Attribute{};
// This attribute is only valid on a field.
[AttributeUsage(AttributeTargets::Field)]
public ref class FieldTargetAttribute: public Attribute{};
// This attribute is valid on a class or a method.
[AttributeUsage(AttributeTargets::Class|AttributeTargets::Method)]
public ref class ClassMethodTargetAttribute: public Attribute{};
// This attribute is valid on any target.
[AttributeUsage(AttributeTargets::All)]
public ref class AllTargetsAttribute: public Attribute{};
[ClassTarget]
[ClassMethodTarget]
[AllTargets]
public ref class TestClassAttribute
{
private:
[ConstructorTarget]
[AllTargets]
TestClassAttribute(){}
public:
[MethodTarget]
[ClassMethodTarget]
[AllTargets]
void Method1(){}
[FieldTarget]
[AllTargets]
int myInt;
static void Main(){}
};
}
using System;
namespace AttTargsCS {
// This attribute is only valid on a class.
[AttributeUsage(AttributeTargets.Class)]
public class ClassTargetAttribute : Attribute {
}
// This attribute is only valid on a method.
[AttributeUsage(AttributeTargets.Method)]
public class MethodTargetAttribute : Attribute {
}
// This attribute is only valid on a constructor.
[AttributeUsage(AttributeTargets.Constructor)]
public class ConstructorTargetAttribute : Attribute {
}
// This attribute is only valid on a field.
[AttributeUsage(AttributeTargets.Field)]
public class FieldTargetAttribute : Attribute {
}
// This attribute is valid on a class or a method.
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
public class ClassMethodTargetAttribute : Attribute {
}
// This attribute is valid on a generic type parameter.
[AttributeUsage(AttributeTargets.GenericParameter)]
public class GenericParameterTargetAttribute : Attribute {
}
// This attribute is valid on any target.
[AttributeUsage(AttributeTargets.All)]
public class AllTargetsAttribute : Attribute {
}
[ClassTarget]
[ClassMethodTarget]
[AllTargets]
public class TestClassAttribute {
[ConstructorTarget]
[AllTargets]
TestClassAttribute() {
}
[MethodTarget]
[ClassMethodTarget]
[AllTargets]
public void Method1() {
}
[FieldTarget]
[AllTargets]
public int myInt;
public void GenericMethod<
[GenericParameterTarget, AllTargets] T>(T x) {
}
static void Main(string[] args) {
}
}
}
Module DemoModule
' This attribute is only valid on a class.
<AttributeUsage(AttributeTargets.Class)> _
Public Class ClassTargetAttribute
Inherits Attribute
End Class
' This attribute is only valid on a method.
<AttributeUsage(AttributeTargets.Method)> _
Public Class MethodTargetAttribute
Inherits Attribute
End Class
' This attribute is only valid on a constructor.
<AttributeUsage(AttributeTargets.Constructor)> _
Public Class ConstructorTargetAttribute
Inherits Attribute
End Class
' This attribute is only valid on a field.
<AttributeUsage(AttributeTargets.Field)> _
Public Class FieldTargetAttribute
Inherits Attribute
End Class
' This attribute is valid on a class or a method.
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)> _
Public Class ClassMethodTargetAttribute
Inherits Attribute
End Class
' This attribute is valid on any target.
<AttributeUsage(AttributeTargets.All)> _
Public Class AllTargetsAttribute
Inherits Attribute
End Class
<ClassTarget, _
ClassMethodTarget, _
AllTargets> _
Public Class TestClassAttribute
<ConstructorTarget, _
AllTargets> _
Public Sub New
End Sub
<MethodTarget, _
ClassMethodTarget, _
AllTargets> _
Public Sub Method1()
End Sub
<FieldTarget, _
AllTargets> _
Public myInt as Integer
End Class
Sub Main()
End Sub
End Module
설명
AttributeUsageAttribute클래스는이 열거형을 사용 하 여 특성을 적용 하는 데 유효한 요소의 종류를 지정 합니다.The AttributeUsageAttribute class uses this enumeration to specify the kind of element on which it is valid to apply an attribute.
AttributeTargets 열거형 값은 비트 OR 연산으로 결합 하 여 기본 조합을 가져올 수 있습니다.AttributeTargets enumeration values can be combined with a bitwise OR operation to get the preferred combination.