1 out of 4 rated this helpful - Rate this topic

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.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[FlagsAttribute] 
public enum AttributeTargets
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute FlagsAttribute() */ 
public enum AttributeTargets
SerializableAttribute 
ComVisibleAttribute(true) 
FlagsAttribute 
public enum AttributeTargets
  Member name Description
Supported by the .NET Compact Framework All Attribute can be applied to any application element. 
Supported by the .NET Compact Framework Assembly Attribute can be applied to an assembly. 
Supported by the .NET Compact Framework Class Attribute can be applied to a class. 
Supported by the .NET Compact Framework Constructor Attribute can be applied to a constructor. 
Supported by the .NET Compact Framework Delegate Attribute can be applied to a delegate. 
Supported by the .NET Compact Framework Enum Attribute can be applied to an enumeration. 
Supported by the .NET Compact Framework Event Attribute can be applied to an event. 
Supported by the .NET Compact Framework Field Attribute can be applied to a field. 
Supported by the .NET Compact Framework GenericParameter Attribute can be applied to a generic parameter. 
Supported by the .NET Compact Framework Interface Attribute can be applied to an interface. 
Supported by the .NET Compact Framework Method Attribute can be applied to a method. 
Supported by the .NET Compact Framework Module Attribute can be applied to a module. 
NoteNote

Module refers to a portable executable file (.dll or.exe) and not a Visual Basic standard module.

Supported by the .NET Compact Framework Parameter Attribute can be applied to a parameter. 
Supported by the .NET Compact Framework Property Attribute can be applied to a property. 
Supported by the .NET Compact Framework ReturnValue Attribute can be applied to a return value. 
Supported by the .NET Compact Framework 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 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) {
		}
	}
}

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ