Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

AttributeTargets Enumeration

Specifies the application elements on which it is valid to apply an attribute.

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

[SerializableAttribute] 
[FlagsAttribute] 
[ComVisibleAttribute(true)] 
public enum AttributeTargets
/** @attribute SerializableAttribute() */ 
/** @attribute FlagsAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum AttributeTargets
SerializableAttribute 
FlagsAttribute 
ComVisibleAttribute(true) 
public enum AttributeTargets
 Member nameDescription
Supported by the .NET Compact FrameworkAllAttribute can be applied to any application element. 
Supported by the .NET Compact FrameworkAssemblyAttribute can be applied to an assembly. 
Supported by the .NET Compact FrameworkClassAttribute can be applied to a class. 
Supported by the .NET Compact FrameworkConstructorAttribute can be applied to a constructor. 
Supported by the .NET Compact FrameworkDelegateAttribute can be applied to a delegate. 
Supported by the .NET Compact FrameworkEnumAttribute can be applied to an enumeration. 
Supported by the .NET Compact FrameworkEventAttribute can be applied to an event. 
Supported by the .NET Compact FrameworkFieldAttribute can be applied to a field. 
Supported by the .NET Compact FrameworkGenericParameterAttribute can be applied to a generic parameter. 
Supported by the .NET Compact FrameworkInterfaceAttribute can be applied to an interface. 
Supported by the .NET Compact FrameworkMethodAttribute can be applied to a method. 
Supported by the .NET Compact FrameworkModuleAttribute 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 FrameworkParameterAttribute can be applied to a parameter. 
Supported by the .NET Compact FrameworkPropertyAttribute can be applied to a property. 
Supported by the .NET Compact FrameworkReturnValueAttribute can be applied to a return value. 
Supported by the .NET Compact FrameworkStructAttribute 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 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.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.