AttributeTargets Enumeration
Specifies the application elements on which it is valid to apply an attribute.
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
[Visual Basic] <Flags> <Serializable> Public Enum AttributeTargets [C#] [Flags] [Serializable] public enum AttributeTargets [C++] [Flags] [Serializable] __value public enum AttributeTargets [JScript] public Flags Serializable enum AttributeTargets
Remarks
AttributeTargets is used as a parameter of AttributeUsageAttribute to specify the kind of element on which it is valid to apply an attribute.
AttributeTargets enumeration values can be combined with a bitwise OR operation to get the preferred combination.
Members
| Member name | Description | Value |
|---|---|---|
| All Supported by the .NET Compact Framework. | Attribute can be applied to any application element. | 16383 |
| Assembly Supported by the .NET Compact Framework. | Attribute can be applied to an assembly. | 1 |
| Class Supported by the .NET Compact Framework. | Attribute can be applied to a class. | 4 |
| Constructor Supported by the .NET Compact Framework. | Attribute can be applied to a constructor. | 32 |
| Delegate Supported by the .NET Compact Framework. | Attribute can be applied to a delegate. | 4096 |
| Enum Supported by the .NET Compact Framework. | Attribute can be applied to an enumeration. | 16 |
| Event Supported by the .NET Compact Framework. | Attribute can be applied to an event. | 512 |
| Field Supported by the .NET Compact Framework. | Attribute can be applied to a field. | 256 |
| Interface Supported by the .NET Compact Framework. | Attribute can be applied to an interface. | 1024 |
| Method Supported by the .NET Compact Framework. | Attribute can be applied to a method. | 64 |
| Module Supported by the .NET Compact Framework. | Attribute can be applied to a module.
Note Module refers to a portable executable file (.dll or .exe) and not a Visual Basic standard module. | 2 |
| Parameter Supported by the .NET Compact Framework. | Attribute can be applied to a parameter. | 2048 |
| Property Supported by the .NET Compact Framework. | Attribute can be applied to a property. | 128 |
| ReturnValue Supported by the .NET Compact Framework. | Attribute can be applied to a return value. | 8192 |
| Struct Supported by the .NET Compact Framework. | Attribute can be applied to a structure; that is, a value type. | 8 |
Example
The following code sample illustrates the application of the AttributeTargets enumeration:
[C#] 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 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; static void Main(string[] args) { } } } [C++] #using <mscorlib.dll> using namespace System; namespace AttTargsCS { // This attribute is only valid on a class. [AttributeUsage(AttributeTargets::Class)] public __gc class ClassTargetAttribute : public Attribute { }; // This attribute is only valid on a method. [AttributeUsage(AttributeTargets::Method)] public __gc class MethodTargetAttribute : public Attribute { }; // This attribute is only valid on a constructor. [AttributeUsage(AttributeTargets::Constructor)] public __gc class ConstructorTargetAttribute : public Attribute { }; // This attribute is only valid on a field. [AttributeUsage(AttributeTargets::Field)] public __gc class FieldTargetAttribute : public Attribute { }; // This attribute is valid on a class or a method. [AttributeUsage(AttributeTargets::Class|AttributeTargets::Method)] public __gc class ClassMethodTargetAttribute : public Attribute { }; // This attribute is valid on any target. [AttributeUsage(AttributeTargets::All)] public __gc class AllTargetsAttribute : public Attribute { }; [ClassTarget] [ClassMethodTarget] [AllTargets] public __gc class TestClassAttribute { [ConstructorTarget] [AllTargets] TestClassAttribute() { } public: [MethodTarget] [ClassMethodTarget] [AllTargets] void Method1() { } [FieldTarget] [AllTargets] int myInt; static void Main() { } }; } [JScript] import System; package AttTargsJS { // This attribute is only valid on a class. AttributeUsage(AttributeTargets.Class) public class ClassTargetAttribute extends Attribute { } // This attribute is only valid on a method. AttributeUsage(AttributeTargets.Method) public class MethodTargetAttribute extends Attribute { } // This attribute is only valid on a constructor. AttributeUsage(AttributeTargets.Constructor) public class ConstructorTargetAttribute extends Attribute { } // This attribute is only valid on a field. AttributeUsage(AttributeTargets.Field) public class FieldTargetAttribute extends Attribute { } // This attribute is valid on a class or a method. AttributeUsage(AttributeTargets.Class|AttributeTargets.Method) public class ClassMethodTargetAttribute extends Attribute { } // This attribute is valid on any target. AttributeUsage(AttributeTargets.All) public class AllTargetsAttribute extends Attribute { } ClassTargetAttribute ClassMethodTargetAttribute AllTargetsAttribute public class TestClassAttribute { ConstructorTargetAttribute AllTargetsAttribute function TestClassAttribute() { } MethodTargetAttribute ClassMethodTargetAttribute AllTargetsAttribute public function Method1() { } FieldTargetAttribute AllTargetsAttribute public var myInt : int; static function Main(args : String[]) { } } }
[Visual Basic] No example is available for Visual Basic. To view a C#, C++, or JScript example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: Mscorlib (in Mscorlib.dll)