AttributeTargets Enumeration
.NET Framework 3.0
Specifies the application elements on which it is valid to apply an attribute.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
[SerializableAttribute] [FlagsAttribute] [ComVisibleAttribute(true)] public enum class AttributeTargets
/** @attribute SerializableAttribute() */ /** @attribute FlagsAttribute() */ /** @attribute ComVisibleAttribute(true) */ public enum AttributeTargets
SerializableAttribute FlagsAttribute ComVisibleAttribute(true) public enum AttributeTargets
| Member name | Description | |||
|---|---|---|---|---|
![]() | All | Attribute can be applied to any application element. | ||
![]() | Assembly | Attribute can be applied to an assembly. | ||
![]() | Class | Attribute can be applied to a class. | ||
![]() | Constructor | Attribute can be applied to a constructor. | ||
![]() | Delegate | Attribute can be applied to a delegate. | ||
![]() | Enum | Attribute can be applied to an enumeration. | ||
![]() | Event | Attribute can be applied to an event. | ||
![]() | Field | Attribute can be applied to a field. | ||
![]() | GenericParameter | Attribute can be applied to a generic parameter. | ||
![]() | Interface | Attribute can be applied to an interface. | ||
![]() | Method | Attribute can be applied to a method. | ||
![]() | Module | Attribute can be applied to a module.
| ||
![]() | Parameter | Attribute can be applied to a parameter. | ||
![]() | Property | Attribute can be applied to a property. | ||
![]() | ReturnValue | Attribute can be applied to a return value. | ||
![]() | Struct | Attribute can be applied to a structure; that is, a value type. |
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.
The following code sample illustrates the application of the AttributeTargets enumeration:
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(){} }; }
package AttTargsJSL;
import System.*;
// This attribute is only valid on a class.
/** @attribute AttributeUsage(AttributeTargets.Class)
*/
public class ClassTargetAttribute extends Attribute
{
} //ClassTargetAttribute
// This attribute is only valid on a method.
/** @attribute AttributeUsage(AttributeTargets.Method)
*/
public class MethodTargetAttribute extends Attribute
{
} //MethodTargetAttribute
// This attribute is only valid on a constructor.
/** @attribute AttributeUsage(AttributeTargets.Constructor)
*/
public class ConstructorTargetAttribute extends Attribute
{
} //ConstructorTargetAttribute
// This attribute is only valid on a field.
/** @attribute AttributeUsage(AttributeTargets.Field)
*/
public class FieldTargetAttribute extends Attribute
{
} //FieldTargetAttribute
// This attribute is valid on a class or a method.
/** @attribute AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)
*/
public class ClassMethodTargetAttribute extends Attribute
{
} //ClassMethodTargetAttribute
// This attribute is valid on any target.
/** @attribute AttributeUsage(AttributeTargets.All)
*/
public class AllTargetsAttribute extends Attribute
{
} //AllTargetsAttribute
/** @attribute ClassTarget()
*/
/** @attribute ClassMethodTarget()
*/
/** @attribute AllTargets()
*/
public class TestClassAttribute
{
/** @attribute.ConstructorTarget ConstructorTarget()
*/
/** @attribute AllTargets()
*/
TestClassAttribute()
{
} //TestClassAttribute
/** @attribute MethodTarget()
*/
/** @attribute ClassMethodTarget()
*/
/** @attribute AllTargets()
*/
public void Method1()
{
} //Method1
/** @attribute FieldTarget()
*/
/** @attribute AllTargets()
*/
public int myInt;
public static void main(String[] args)
{
} //main
} //TestClassAttribute
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[]) { } } }
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show:
