Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Attribute Class

Represents the base class for custom attributes.

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

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
[ComVisibleAttribute(true)]
public abstract class Attribute

The Attribute type exposes the following members.

  NameDescription
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360AttributeInitializes a new instance of the Attribute class.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360EqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Overrides Object.Equals(Object).)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttribute(Assembly, Type)Retrieves a custom attribute applied to a specified assembly. Parameters specify the assembly and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttribute(MemberInfo, Type)Retrieves a custom attribute applied to a member of a type. Parameters specify the member, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttribute(Module, Type)Retrieves a custom attribute applied to a module. Parameters specify the module, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttribute(ParameterInfo, Type)Retrieves a custom attribute applied to a method parameter. Parameters specify the method parameter, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttribute(Assembly, Type, Boolean)Retrieves a custom attribute applied to an assembly. Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttribute(MemberInfo, Type, Boolean)Retrieves a custom attribute applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttribute(Module, Type, Boolean)Retrieves a custom attribute applied to a module. Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttribute(ParameterInfo, Type, Boolean)Retrieves a custom attribute applied to a method parameter. Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Assembly)Retrieves an array of the custom attributes applied to an assembly. A parameter specifies the assembly.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(MemberInfo)Retrieves an array of the custom attributes applied to a member of a type. A parameter specifies the member.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Module)Retrieves an array of the custom attributes applied to a module. A parameter specifies the module.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(ParameterInfo)Retrieves an array of the custom attributes applied to a method parameter. A parameter specifies the method parameter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Assembly, Boolean)Retrieves an array of the custom attributes applied to an assembly. Parameters specify the assembly, and an ignored search option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Assembly, Type)Retrieves an array of the custom attributes applied to an assembly. Parameters specify the assembly, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(MemberInfo, Boolean)Retrieves an array of the custom attributes applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(MemberInfo, Type)Retrieves an array of the custom attributes applied to a member of a type. Parameters specify the member, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Module, Boolean)Retrieves an array of the custom attributes applied to a module. Parameters specify the module, and an ignored search option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Module, Type)Retrieves an array of the custom attributes applied to a module. Parameters specify the module, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(ParameterInfo, Boolean)Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method parameter, and whether to search ancestors of the method parameter.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(ParameterInfo, Type)Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method parameter, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Assembly, Type, Boolean)Retrieves an array of the custom attributes applied to an assembly. Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(MemberInfo, Type, Boolean)Retrieves an array of the custom attributes applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(Module, Type, Boolean)Retrieves an array of the custom attributes applied to a module. Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GetCustomAttributes(ParameterInfo, Type, Boolean)Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeReturns the hash code for this instance. (Overrides Object.GetHashCode().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefined(Assembly, Type)Determines whether any custom attributes are applied to an assembly. Parameters specify the assembly, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefined(MemberInfo, Type)Determines whether any custom attributes are applied to a member of a type. Parameters specify the member, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefined(Module, Type)Determines whether any custom attributes of a specified type are applied to a module. Parameters specify the module, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefined(ParameterInfo, Type)Determines whether any custom attributes are applied to a method parameter. Parameters specify the method parameter, and the type of the custom attribute to search for.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefined(Assembly, Type, Boolean)Determines whether any custom attributes are applied to an assembly. Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefined(MemberInfo, Type, Boolean)Determines whether any custom attributes are applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefined(Module, Type, Boolean)Determines whether any custom attributes are applied to a module. Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360IsDefined(ParameterInfo, Type, Boolean)Determines whether any custom attributes are applied to a method parameter. Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object.
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The Attribute class associates predefined system information or user-defined custom information with a target element. A target element can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute.

Information provided by an attribute is also known as metadata. Metadata can be examined at run time by your application to control how your program processes data, or before run time by external tools to control how your application itself is processed or maintained. For example, the .NET Framework predefines and uses attribute types to control run-time behavior, and some programming languages use attribute types to represent language features not directly supported by the .NET Framework common type system.

All attribute types derive directly or indirectly from the Attribute class. Attributes can be applied to any target element; multiple attributes can be applied to the same target element; and attributes can be inherited by an element derived from a target element. Use the AttributeTargets class to specify the target element to which the attribute is applied.

The Attribute class provides convenient methods to retrieve and test custom attributes.

The following code example demonstrates the usage of Attribute.


using System;
using System.Reflection;

// An enumeration of animals. Start at 1 (0 = uninitialized).
public enum Animal
{
   // Pets.
   Dog = 1,
   Cat,
   Bird,
}

// A custom attribute to allow a target to have a pet.
public class AnimalTypeAttribute : Attribute
{
   // The constructor is called when the attribute is set.
   public AnimalTypeAttribute(Animal pet)
   {
      thePet = pet;
   }

   // Keep a variable internally ...
   protected Animal thePet;

   // .. and show a copy to the outside world.
   public Animal Pet
   {
      get { return thePet; }
      set { thePet = Pet; }
   }
}

// A test class where each method has its own pet.
class AnimalTypeTestClass
{
   [AnimalType(Animal.Dog)]
   public void DogMethod() { }

   [AnimalType(Animal.Cat)]
   public void CatMethod() { }

   [AnimalType(Animal.Bird)]
   public void BirdMethod() { }
}

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      AnimalTypeTestClass testClass = new AnimalTypeTestClass();
      Type type = testClass.GetType();
      // Iterate through all the methods of the class.
      foreach (MethodInfo mInfo in type.GetMethods())
      {
         // Iterate through all the Attributes for each method.
         foreach (Attribute attr in
             Attribute.GetCustomAttributes(mInfo))
         {
            // Check for the AnimalType attribute.
            if (attr.GetType() == typeof(AnimalTypeAttribute))
               outputBlock.Text += String.Format(
                   "Method {0} has a pet {1} attribute.",
                   mInfo.Name, ((AnimalTypeAttribute)attr).Pet) + "\n";
         }

      }
   }
}
/*
 * Output:
 * Method DogMethod has a pet Dog attribute.
 * Method CatMethod has a pet Cat attribute.
 * Method BirdMethod has a pet Bird attribute.
 */


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

This type is thread safe.

System.Object
  System.Attribute
    Microsoft.Internal.Pivot.Utilities.SerializableAttribute
    Microsoft.Internal.Pivot.Utilities.XmlAnyAttributeAttribute
    Microsoft.VisualBasic.CompilerServices.DesignerGeneratedAttribute
    Microsoft.VisualBasic.CompilerServices.OptionCompareAttribute
    Microsoft.VisualBasic.CompilerServices.OptionTextAttribute
    Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute
    Microsoft.VisualBasic.HideModuleNameAttribute
    Microsoft.VisualBasic.MyGroupCollectionAttribute
    System.AttributeUsageAttribute
    System.CLSCompliantAttribute
    System.CodeDom.Compiler.GeneratedCodeAttribute
    System.ComponentModel.AlternateContentPropertyAttribute
    System.ComponentModel.BindableAttribute
    System.ComponentModel.BrowsableAttribute
    System.ComponentModel.CategoryAttribute
    System.ComponentModel.ComplexBindingPropertiesAttribute
    System.ComponentModel.Composition.ExportAttribute
    System.ComponentModel.Composition.ExportMetadataAttribute
    System.ComponentModel.Composition.ImportAttribute
    System.ComponentModel.Composition.ImportingConstructorAttribute
    System.ComponentModel.Composition.ImportManyAttribute
    System.ComponentModel.Composition.MetadataAttributeAttribute
    System.ComponentModel.Composition.PartCreationPolicyAttribute
    System.ComponentModel.Composition.PartMetadataAttribute
    System.ComponentModel.Composition.PartNotDiscoverableAttribute
    System.ComponentModel.DataAnnotations.AssociationAttribute
    System.ComponentModel.DataAnnotations.ConcurrencyCheckAttribute
    System.ComponentModel.DataAnnotations.DisplayAttribute
    System.ComponentModel.DataAnnotations.DisplayColumnAttribute
    System.ComponentModel.DataAnnotations.DisplayFormatAttribute
    System.ComponentModel.DataAnnotations.EditableAttribute
    System.ComponentModel.DataAnnotations.FilterUIHintAttribute
    System.ComponentModel.DataAnnotations.KeyAttribute
    System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute
    System.ComponentModel.DataAnnotations.TimestampAttribute
    System.ComponentModel.DataAnnotations.UIHintAttribute
    System.ComponentModel.DataAnnotations.ValidationAttribute
    System.ComponentModel.DefaultBindingPropertyAttribute
    System.ComponentModel.DefaultEventAttribute
    System.ComponentModel.DefaultPropertyAttribute
    System.ComponentModel.DefaultValueAttribute
    System.ComponentModel.DescriptionAttribute
    System.ComponentModel.DesignTimeVisibleAttribute
    System.ComponentModel.EditorBrowsableAttribute
    System.ComponentModel.LookupBindingPropertiesAttribute
    System.ComponentModel.ReadOnlyAttribute
    System.ComponentModel.TypeConverterAttribute
    System.ContextStaticAttribute
    System.Data.Services.Client.MediaEntryAttribute
    System.Data.Services.Client.MimeTypePropertyAttribute
    System.Data.Services.Common.DataServiceEntityAttribute
    System.Data.Services.Common.DataServiceKeyAttribute
    System.Data.Services.Common.EntityPropertyMappingAttribute
    System.Data.Services.Common.EntitySetAttribute
    System.Data.Services.Common.HasStreamAttribute
    System.Diagnostics.CodeAnalysis.SuppressMessageAttribute
    System.Diagnostics.ConditionalAttribute
    System.Diagnostics.Contracts.ContractClassAttribute
    System.Diagnostics.Contracts.ContractClassForAttribute
    System.Diagnostics.Contracts.ContractInvariantMethodAttribute
    System.Diagnostics.Contracts.ContractPublicPropertyNameAttribute
    System.Diagnostics.Contracts.ContractReferenceAssemblyAttribute
    System.Diagnostics.Contracts.ContractRuntimeIgnoredAttribute
    System.Diagnostics.Contracts.ContractVerificationAttribute
    System.Diagnostics.Contracts.PureAttribute
    System.Diagnostics.DebuggableAttribute
    System.Diagnostics.DebuggerBrowsableAttribute
    System.Diagnostics.DebuggerDisplayAttribute
    System.Diagnostics.DebuggerHiddenAttribute
    System.Diagnostics.DebuggerNonUserCodeAttribute
    System.Diagnostics.DebuggerStepThroughAttribute
    System.Diagnostics.DebuggerTypeProxyAttribute
    System.FlagsAttribute
    System.ObsoleteAttribute
    System.ParamArrayAttribute
    System.Reflection.AssemblyAlgorithmIdAttribute
    System.Reflection.AssemblyCompanyAttribute
    System.Reflection.AssemblyConfigurationAttribute
    System.Reflection.AssemblyCopyrightAttribute
    System.Reflection.AssemblyCultureAttribute
    System.Reflection.AssemblyDefaultAliasAttribute
    System.Reflection.AssemblyDelaySignAttribute
    System.Reflection.AssemblyDescriptionAttribute
    System.Reflection.AssemblyFileVersionAttribute
    System.Reflection.AssemblyFlagsAttribute
    System.Reflection.AssemblyInformationalVersionAttribute
    System.Reflection.AssemblyKeyFileAttribute
    System.Reflection.AssemblyKeyNameAttribute
    System.Reflection.AssemblyProductAttribute
    System.Reflection.AssemblyTitleAttribute
    System.Reflection.AssemblyTrademarkAttribute
    System.Reflection.AssemblyVersionAttribute
    System.Reflection.DefaultMemberAttribute
    System.Resources.NeutralResourcesLanguageAttribute
    System.Resources.SatelliteContractVersionAttribute
    System.Runtime.CompilerServices.AccessedThroughPropertyAttribute
    System.Runtime.CompilerServices.CompilationRelaxationsAttribute
    System.Runtime.CompilerServices.CompilerGeneratedAttribute
    System.Runtime.CompilerServices.CustomConstantAttribute
    System.Runtime.CompilerServices.DecimalConstantAttribute
    System.Runtime.CompilerServices.DynamicAttribute
    System.Runtime.CompilerServices.ExtensionAttribute
    System.Runtime.CompilerServices.FixedAddressValueTypeAttribute
    System.Runtime.CompilerServices.FixedBufferAttribute
    System.Runtime.CompilerServices.IndexerNameAttribute
    System.Runtime.CompilerServices.InternalsVisibleToAttribute
    System.Runtime.CompilerServices.MethodImplAttribute
    System.Runtime.CompilerServices.NativeCppClassAttribute
    System.Runtime.CompilerServices.ReferenceAssemblyAttribute
    System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
    System.Runtime.CompilerServices.SpecialNameAttribute
    System.Runtime.CompilerServices.TypeForwardedFromAttribute
    System.Runtime.CompilerServices.TypeForwardedToAttribute
    System.Runtime.CompilerServices.UnsafeValueTypeAttribute
    System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute
    System.Runtime.InteropServices.AllowReversePInvokeCallsAttribute
    System.Runtime.InteropServices.ClassInterfaceAttribute
    System.Runtime.InteropServices.CoClassAttribute
    System.Runtime.InteropServices.ComDefaultInterfaceAttribute
    System.Runtime.InteropServices.ComImportAttribute
    System.Runtime.InteropServices.ComVisibleAttribute
    System.Runtime.InteropServices.DefaultCharSetAttribute
    System.Runtime.InteropServices.DispIdAttribute
    System.Runtime.InteropServices.DllImportAttribute
    System.Runtime.InteropServices.FieldOffsetAttribute
    System.Runtime.InteropServices.GuidAttribute
    System.Runtime.InteropServices.InAttribute
    System.Runtime.InteropServices.InterfaceTypeAttribute
    System.Runtime.InteropServices.MarshalAsAttribute
    System.Runtime.InteropServices.OptionalAttribute
    System.Runtime.InteropServices.OutAttribute
    System.Runtime.InteropServices.PreserveSigAttribute
    System.Runtime.InteropServices.StructLayoutAttribute
    System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute
    System.Runtime.Serialization.CollectionDataContractAttribute
    System.Runtime.Serialization.ContractNamespaceAttribute
    System.Runtime.Serialization.DataContractAttribute
    System.Runtime.Serialization.DataMemberAttribute
    System.Runtime.Serialization.EnumMemberAttribute
    System.Runtime.Serialization.IgnoreDataMemberAttribute
    System.Runtime.Serialization.KnownTypeAttribute
    System.Runtime.Serialization.OnDeserializedAttribute
    System.Runtime.Serialization.OnDeserializingAttribute
    System.Runtime.Serialization.OnSerializedAttribute
    System.Runtime.Serialization.OnSerializingAttribute
    System.Runtime.Versioning.ComponentGuaranteesAttribute
    System.Runtime.Versioning.ResourceExposureAttribute
    System.Runtime.Versioning.TargetFrameworkAttribute
    System.Security.AllowPartiallyTrustedCallersAttribute
    System.Security.Permissions.SecurityAttribute
    System.Security.SecurityCriticalAttribute
    System.Security.SecuritySafeCriticalAttribute
    System.Security.SecurityTransparentAttribute
    System.Security.UnverifiableCodeAttribute
    System.ServiceModel.CallbackBehaviorAttribute
    System.ServiceModel.DataContractFormatAttribute
    System.ServiceModel.FaultContractAttribute
    System.ServiceModel.MessageContractAttribute
    System.ServiceModel.MessageContractMemberAttribute
    System.ServiceModel.MessageParameterAttribute
    System.ServiceModel.OperationContractAttribute
    System.ServiceModel.ServiceContractAttribute
    System.ServiceModel.ServiceKnownTypeAttribute
    System.ServiceModel.Web.WebGetAttribute
    System.ServiceModel.Web.WebInvokeAttribute
    System.ServiceModel.XmlSerializerFormatAttribute
    System.STAThreadAttribute
    System.ThreadStaticAttribute
    System.Windows.Browser.ScriptableMemberAttribute
    System.Windows.Browser.ScriptableTypeAttribute
    System.Windows.Markup.ContentPropertyAttribute
    System.Windows.Markup.XmlnsDefinitionAttribute
    System.Windows.Markup.XmlnsPrefixAttribute
    System.Windows.StyleTypedPropertyAttribute
    System.Windows.TemplatePartAttribute
    System.Windows.TemplateVisualStateAttribute
    System.Xml.Serialization.XmlAnyElementAttribute
    System.Xml.Serialization.XmlArrayAttribute
    System.Xml.Serialization.XmlArrayItemAttribute
    System.Xml.Serialization.XmlAttributeAttribute
    System.Xml.Serialization.XmlChoiceIdentifierAttribute
    System.Xml.Serialization.XmlElementAttribute
    System.Xml.Serialization.XmlEnumAttribute
    System.Xml.Serialization.XmlIgnoreAttribute
    System.Xml.Serialization.XmlIncludeAttribute
    System.Xml.Serialization.XmlNamespaceDeclarationsAttribute
    System.Xml.Serialization.XmlRootAttribute
    System.Xml.Serialization.XmlSchemaProviderAttribute
    System.Xml.Serialization.XmlTextAttribute
    System.Xml.Serialization.XmlTypeAttribute

Community Additions

Show:
© 2014 Microsoft