public abstract class Attribute
Object
Attribute
mscorlib
BCL
Serves as the base class for custom attributes.
All attributes, whether built-in or user-defined, derive directly or indirectly from Attribute. Attributes inherit certain default behaviors: the attribute may be associated with any target element (see AttributeTargets); may or may not be inherited by a derived element; and multiple instances may or may not be allowed on the same target element. These behaviors are specified using AttributeUsageAttribute.[Note: An attribute is an annotation that may be placed on an element of source code and used to store application-specific information at compile time. This information is stored in the metadata and can be accessed either during application execution, through a process known as reflection, or when another tool reads the metadata. Attributes might change the behavior of the application during execution, provide transaction information about an object, or convey organizational information to a designer. ]
The CLI predefines some attribute types and uses them to control runtime behavior. Some languages predefine attribute types to represent language features not directly represented in the Common Language Specification (CLS). User-defined attribute classes, inheriting from Attribute, may also be created. The definition of such a class includes the name of the attribute, its default behavior, and the information to be stored.
The following example creates and assigns multiple custom attributes to a class. The attribute contains the name of the programmer and the version number of the class.
using System; [AttributeUsage(AttributeTargets.Class| AttributeTargets.Struct, AllowMultiple=true)] public class Author : Attribute { string authorName; public double verSion; public Author(string name) { authorName = name; verSion = 1.0; } public string getName() { return authorName; } } [Author("Some Author")] class FirstClass { /*...*/ } class SecondClass // no Author attribute { /*...*/ } [Author("Some Author"), Author("Some Other Author", verSion=1.1)] class ThirdClass { /*...*/ } class AuthorInfo { public static void Main() { PrintAuthorInfo(typeof(FirstClass)); PrintAuthorInfo(typeof(SecondClass)); PrintAuthorInfo(typeof(ThirdClass)); } public static void PrintAuthorInfo(Type type) { Console.WriteLine("Author information for {0}", type); Attribute[] attributeArray = Attribute.GetCustomAttributes(type); foreach(Attribute attrib in attributeArray) { if (attrib is Author) { Author author = (Author)attrib; Console.WriteLine(" {0}, version {1:f}", author.getName(), author.verSion); } } Console.WriteLine(); } }The output is
Author information for FirstClass
Some Author, version 1.00
Author information for SecondClass
Author information for ThirdClass
Some Author, version 1.00
Some Other Author, version 1.10
AttributeUsageAttribute(AttributeTargets.All, AllowMultiple=false, Inherited=true)
System Namespace
Attribute Constructors
Attribute Methods
Attribute.Equals Method
Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type) Method
Attribute.GetCustomAttribute(System.Reflection.ParameterInfo, System.Type) Method
Attribute.GetCustomAttribute(System.Reflection.Module, System.Type) Method
Attribute.GetCustomAttribute(System.Reflection.Assembly, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.MemberInfo) Method
Attribute.GetCustomAttributes(System.Reflection.ParameterInfo, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.Module, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.Assembly, System.Type) Method
Attribute.GetCustomAttributes(System.Reflection.ParameterInfo) Method
Attribute.GetCustomAttributes(System.Reflection.Module) Method
Attribute.GetCustomAttributes(System.Reflection.Assembly) Method
Attribute.GetHashCode Method
Attribute.IsDefined(System.Reflection.MemberInfo, System.Type) Method
Attribute.IsDefined(System.Reflection.ParameterInfo, System.Type) Method
Attribute.IsDefined(System.Reflection.Module, System.Type) Method
Attribute.IsDefined(System.Reflection.Assembly, System.Type) Method
protected Attribute();
Constructs a new instance of the Attribute class.
System.Attribute Class, System Namespace
public override bool Equals(object obj);
Determines whether the current instance and the specified Object represent the same type and value.
- obj
- The Object to compare to the current instance.
A Boolean wheretrue
indicates obj represents the same type and value as the current instance. If obj is a null reference or is not an instance of Attribute, returnsfalse
.
[Note: This method overrides System.Object.Equals(System.Object).]
System.Attribute Class, System Namespace
public static Attribute GetCustomAttribute(MemberInfo element, Type attributeType);
Returns an instance of a specified custom attribute if a single instance of the attribute is in the metadata for the specified member.
- element
- An instance of a type derived from MemberInfo that describes a type member.
- attributeType
- The Type of the custom attribute for which to check.
The single instance of Attribute of type attributeType that is applied to element. Returnsnull
if the specified attribute was not found.
Exception Type Condition ArgumentNullException element or attributeType is null
.ArgumentException attributeType is not a type derived from Attribute. NotSupportedException element does not represent a constructor, method, property, event, type, or field member. AmbiguousMatchException More than one instance of the specified custom attribute was found.
[Note: If multiple instances of attributeType can be applied to element, use System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type).]
Reflection
System.Attribute Class, System Namespace
public static Attribute GetCustomAttribute(ParameterInfo element, Type attributeType);
Returns an instance of a specified custom attribute if a single instance of the attribute is in the metadata for the specified parameter.
- element
- A ParameterInfo instance.
- attributeType
- The Type of the custom attribute for which to check.
The single instance of Attribute of type attributeType that is applied to element. Returnsnull
if the specified attribute was not found.
Exception Type Condition ArgumentNullException element or attributeType is null
.ArgumentException attributeType is not a type derived from Attribute. AmbiguousMatchException More than one instance of the specified custom attribute was found.
[Note: If multiple instances of attributeType can be applied to element, use System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type).]
Reflection
System.Attribute Class, System Namespace
public static Attribute GetCustomAttribute(Module element, Type attributeType);
Returns an instance of a specified custom attribute if a single instance of the attribute is in the metadata for the specified module.
- element
- A Module instance.
- attributeType
- The Type of the custom attribute for which to check.
The single instance of Attribute of type attributeType that is applied to element. Returnsnull
if the specified attribute was not found.
Exception Type Condition ArgumentNullException element or attributeType is null
.ArgumentException attributeType is not a type derived from Attribute. AmbiguousMatchException More than one instance of the specified custom attribute was found.
[Note: If multiple instances of attributeType can be applied to element, use System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type).]
Reflection
System.Attribute Class, System Namespace
public static Attribute GetCustomAttribute(Assembly element, Type attributeType);
Returns an instance of a specified custom attribute if a single instance of the attribute is in the metadata for the specified assembly.
- element
- A Assembly instance.
- attributeType
- The Type of the custom attribute for which to check.
The single instance of Attribute of type attributeType that is applied to element. Returnsnull
if the specified attribute was not found.
Exception Type Condition ArgumentNullException element or attributeType is null
.ArgumentException attributeType is not a type derived from Attribute. AmbiguousMatchException More than one instance of the specified custom attribute was found.
[Note: If multiple instances of attributeType can be applied to element, use System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type).]
Reflection
System.Attribute Class, System Namespace
public static Attribute[] GetCustomAttributes(MemberInfo element, Type type);
Returns an array of the instances of a specified custom attribute if the attribute is in the metadata for the specified member.
- element
- An instance of a type derived from MemberInfo that describes a type member.
- type
- The Type of the custom attribute for which to check.
An array of type type containing the instances that are applied to element. The array includes instances of type that are inherited by element, if any. Returns an empty array if the specified attribute was not found.
Exception Type Condition ArgumentNullException element or type is null
.ArgumentException type is not a type derived from Attribute. NotSupportedException element does not represent a constructor, method, property, event, type, or field member.
Reflection
System.Attribute Class, System Namespace
public static Attribute[] GetCustomAttributes(MemberInfo element);
Returns an array of all custom attributes in the metadata for the specified member.
- element
- An instance of a type derived from MemberInfo that describes a type member.
A Attribute array containing all custom attributes that are applied to element. The array includes custom attributes that are inherited by element, if any. Returns an empty array if no custom attributes were found in the metadata for element .
Exception Type Condition ArgumentNullException element is null
.NotSupportedException element does not represent a constructor, method, property, event, type, or field member.
Reflection
System.Attribute Class, System Namespace
public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attributeType);
Returns an array of the instances of a specified custom attribute if the attribute is in the metadata for the specified parameter.
- element
- A ParameterInfo instance.
- attributeType
- The Type of the custom attribute for which to check.
An array of type attributeType containing the instances that are applied to element. The array includes any inherited instances of attributeType. Returns an empty array if the specified attribute was not found.
Exception Type Condition ArgumentNullException element or type is null
.ArgumentException attributeType is not a type derived from Attribute.
If element represents a method parameter, the array returned by System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo,System.Type) includes any attributeType instances for the parameter element in the base methods.
Reflection
System.Attribute Class, System Namespace
public static Attribute[] GetCustomAttributes(Module element, Type attributeType);
Returns an array of the instances of a specified custom attribute if the attribute is in the metadata for the specified module.
- element
- A Module instance.
- attributeType
- The Type of the custom attribute for which to check.
An array of type attributeType containing the instances that are applied to element. The array includes any inherited instances of attributeType. Returns an empty array if the specified attribute was not found.
Exception Type Condition ArgumentNullException element or type is null
.ArgumentException attributeType is not a type derived from Attribute.
Reflection
System.Attribute Class, System Namespace
public static Attribute[] GetCustomAttributes(Assembly element, Type attributeType);
Returns an array of the instances of a specified custom attribute if the attribute is in the metadata for the specified assembly.
- element
- A Assembly instance.
- attributeType
- The Type of the custom attribute for which to check.
An array of type attributeType containing the instances that are applied to element. The array includes any inherited instances of attributeType. Returns an empty array if the specified attribute was not found.
Exception Type Condition ArgumentNullException element or type is null
.ArgumentException attributeType is not a type derived from Attribute.
Reflection
System.Attribute Class, System Namespace
public static Attribute[] GetCustomAttributes(ParameterInfo element);
Returns an array of all custom attributes in the metadata for the specified parameter.
- element
- A ParameterInfo instance.
A Attribute array containing all custom attributes that are applied to element. The array includes any inherited custom attributes. Returns an empty array if no custom attributes were found in the metadata for element .
Exception Type Condition ArgumentNullException element is null
.
Reflection
System.Attribute Class, System Namespace
public static Attribute[] GetCustomAttributes(Module element);
Returns an array of all custom attributes in the metadata for the specified module.
- element
- A Module instance.
A Attribute array containing all custom attributes that are applied to element. The array includes any inherited custom attributes. Returns an empty array if no custom attributes were found in the metadata for element .
Exception Type Condition ArgumentNullException element is null
.
Reflection
System.Attribute Class, System Namespace
public static Attribute[] GetCustomAttributes(Assembly element);
Returns an array of all custom attributes in the metadata for the specified assembly.
- element
- A Assembly instance.
A Attribute array containing all custom attributes that are applied to element. The array includes any inherited custom attributes. Returns an empty array if no custom attributes were found in the metadata for element .
Exception Type Condition ArgumentNullException element is null
.
Reflection
System.Attribute Class, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance.
A Int32 containing the hash code for the current instance.
The algorithm used to generate the hash code is unspecified.[Note: This method overrides System.Object.GetHashCode. ]
System.Attribute Class, System Namespace
public static bool IsDefined(MemberInfo element, Type attributeType);
Returns a Boolean value indicating whether a specified custom attribute is present in the metadata for the specified member.
- element
- An instance of a type derived from MemberInfo that describes a type member.
- attributeType
- The Type of the custom attribute for which to check.
true
if a custom attribute of type attributeType is applied to element either directly or through inheritance; otherwise,false
.
Exception Type Condition ArgumentNullException element or attributeType is null
.ArgumentException attributeType is not derived from Attribute. NotSupportedException element is not a constructor, method, property, event, type, or field.
Reflection
System.Attribute Class, System Namespace
public static bool IsDefined(ParameterInfo element, Type attributeType);
Returns a Boolean value indicating whether a specified custom attribute is present in the metadata for the specified parameter.
- element
- A ParameterInfo instance.
- attributeType
- The Type of the custom attribute for which to check.
true
if a custom attribute of type attributeType is applied to element either directly or through inheritance; otherwise,false
.
Exception Type Condition ArgumentNullException element or attributeType is null
.ArgumentException attributeType is not derived from Attribute.
Reflection
System.Attribute Class, System Namespace
public static bool IsDefined(Module element, Type attributeType);
Returns a Boolean value indicating whether a specified custom attribute is present in the metadata for the specified module.
- element
- A Module instance.
- attributeType
- The Type of the custom attribute for which to check.
true
if a custom attribute of type attributeType is applied to element ; otherwise,false
.
Exception Type Condition ArgumentNullException element or attributeType is null
.ArgumentException attributeType is not derived from Attribute.
Reflection
System.Attribute Class, System Namespace
public static bool IsDefined(Assembly element, Type attributeType);
Returns a Boolean value indicating whether a specified custom attribute is present in the metadata for the specified assembly.
- element
- A Assembly instance.
- attributeType
- The Type of the custom attribute for which to check.
true
if a custom attribute of type attributeType is applied to element; otherwise,false
.
Exception Type Condition ArgumentNullException element or attributeType is null
.ArgumentException attributeType is not derived from Attribute.
Reflection
System.Attribute Class, System Namespace