Attribute

Attribute CF 1.0, ECMA 1.0, serializable

System (mscorlib.dll) abstract class

This is the base class for all custom attributes. Attributes are the .NET programmer's means of inserting additional metadata into a type's definition. For example, the .NET Serialization mechanism uses an attribute to indicate which fields in a type should not be serialized (see the System.Reflection.FieldAttributes.NotSerialized enumeration value). .NET programmers are free to create their own attributes (called custom attributes , although from a certain perspective all attributes are inherently custom) by creating a new type that claims Attribute as its base class type.

By themselves , attributes offer no modification to a type's behavioral semantics; that is, attributes don't modify the type's methods or execution in any way. In fact, attribute instances aren't even created until they are retrieved out of the type's metadata via the Reflection APIs. The entire purpose of an attribute is to act as a marker inside the type's metadata for consumption by some other API, library, or facility. For example, the Serialization APIs in the .NET Framework Class Library use the Serializable attribute to indicate which types are serializable. However, by themselves, the attributes carry no code to perform the actual act of serialization. This must be done by passing the instance of the type into instances of the Serialization classes, in which the attribute is retrieved and examined, and "appropriate" action is taken.

Attributes can be attached to any metadata component in the .NET system. This means fields, methods, properties, events, types (classes and value types), assemblies, modules, and more can all be the target of attribute declarations. (An attribute indicates which types it is restricted to by using the AttributeTargets enumeration.)

The base Attribute class provides helper functions for testing custom attributes, including the IsDefined( ) method, which examines a code element and indicates whether its metadata is decorated with a specified type of attribute. To use this method, you must provide the element using the appropriate reflection type (e.g., System.Reflection.Assembly or System.Reflection.ParameterInfo ). You can also use the GetCustomAttribute( ) method to get a reference to an attribute of a specified type, or the GetCustomAttributes( ) method to get an array that contains all matching attributes. When applied to a class or class member, these methods consider all ancestors . To disable this default behavior, use one of the overloaded methods that allows you to supply the inherit parameter, and set it to false .

Custom attributes should override the TypeId property so that it supplies a user -defined identifier that uniquely describes the instance of this attribute on the type. This is entirely because more than one instance of an attribute can be associated with any particular metadata-token (field, type, parameter, and so on) instance.

 public abstract class  Attribute  {  // Protected Constructors  protected  Attribute  ( );  // Public Instance Properties  public virtual object  TypeId  {get; }  // Public Static Methods  public static Attribute  GetCustomAttribute  (System.Reflection.Assembly   element   ,        Type   attributeType   );    public static Attribute  GetCustomAttribute  (System.Reflection.Assembly   element   ,        Type   attributeType   , bool   inherit   );    public static Attribute  GetCustomAttribute  (System.Reflection.MemberInfo   element   ,        Type   attributeType   );    public static Attribute  GetCustomAttribute  (System.Reflection.MemberInfo   element   ,        Type   attributeType   , bool   inherit   );    public static Attribute  GetCustomAttribute  (System.Reflection.Module   element   ,        Type   attributeType   );    public static Attribute  GetCustomAttribute  (System.Reflection.Module   element   ,        Type   attributeType   , bool   inherit   );    public static Attribute  GetCustomAttribute  (System.Reflection.ParameterInfo   element   ,        Type   attributeType   );    public static Attribute  GetCustomAttribute  (System.Reflection.ParameterInfo   element   ,        Type   attributeType   , bool   inherit   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.Assembly   element   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.Assembly   element   ,        bool   inherit   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.Assembly   element   ,        Type   attributeType   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.Assembly   element   ,        Type   attributeType   , bool   inherit   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.MemberInfo   element   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.MemberInfo   element   ,        bool   inherit   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.MemberInfo   element   ,        Type   type   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.MemberInfo   element   ,        Type   type   , bool   inherit   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.Module   element   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.Module   element   ,        bool   inherit   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.Module   element   ,        Type   attributeType   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.Module   element   ,        Type   attributeType   , bool   inherit   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.ParameterInfo   element   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.ParameterInfo   element   ,        bool   inherit   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.ParameterInfo   element   ,        Type   attributeType   );    public static Attribute[ ]  GetCustomAttributes  (System.Reflection.ParameterInfo   element   ,        Type   attributeType   , bool   inherit   );    public static bool  IsDefined  (System.Reflection.Assembly   element   , Type   attributeType   );    public static bool  IsDefined  (System.Reflection.Assembly   element   , Type   attributeType   , bool   inherit   );    public static bool  IsDefined  (System.Reflection.MemberInfo   element   , Type   attributeType   );    public static bool  IsDefined  (System.Reflection.MemberInfo   element   , Type   attributeType   , bool   inherit   );    public static bool  IsDefined  (System.Reflection.Module   element   , Type   attributeType   );    public static bool  IsDefined  (System.Reflection.Module   element   , Type   attributeType   , bool   inherit   );    public static bool  IsDefined  (System.Reflection.ParameterInfo   element   , Type   attributeType   );    public static bool  IsDefined  (System.Reflection.ParameterInfo   element   , Type   attributeType   , bool   inherit   );  // Public Instance Methods  public override bool  Equals  (object   obj   );  // overrides object  public override int  GetHashCode  ( );  // overrides object  public virtual bool  IsDefaultAttribute  ( );    public virtual bool  Match  (object   obj   ); } 

Subclasses

Multiple types

Valid On

All



C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net