Export (0) Print
Expand All

Attribute Class

Represents the base class for custom attributes.

Namespace:  System
Assemblies:   System.Runtime (in System.Runtime.dll)
  mscorlib (in mscorlib.dll)

'Declaration
<AttributeUsageAttribute(AttributeTargets.All, Inherited := True, AllowMultiple := False)> _
Public MustInherit Class Attribute

The Attribute type exposes the following members.

  NameDescription
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsAttributeInitializes a new instance of the Attribute class.
Top

  NameDescription
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Overrides Object.Equals(Object).)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryGetCustomAttribute(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttribute(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 the XNA FrameworkGetCustomAttribute(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttribute(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 the XNA FrameworkGetCustomAttribute(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttribute(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 the XNA FrameworkGetCustomAttribute(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttribute(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(Assembly)Retrieves an array of the custom attributes applied to an assembly. A parameter specifies the assembly.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(MemberInfo)Retrieves an array of the custom attributes applied to a member of a type. A parameter specifies the member.
Public methodStatic memberSupported by the XNA FrameworkGetCustomAttributes(Module)Retrieves an array of the custom attributes applied to a module. A parameter specifies the module.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(ParameterInfo)Retrieves an array of the custom attributes applied to a method parameter. A parameter specifies the method parameter.
Public methodStatic memberSupported by the XNA FrameworkGetCustomAttributes(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(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 the XNA FrameworkGetCustomAttributes(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 the XNA FrameworkGetCustomAttributes(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(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 the XNA FrameworkGetCustomAttributes(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(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 the XNA FrameworkGetCustomAttributes(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 the XNA FrameworkSupported by Portable Class LibraryGetCustomAttributes(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 the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeReturns the hash code for this instance. (Overrides Object.GetHashCode.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryIsDefined(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 the XNA FrameworkSupported by Portable Class LibraryIsDefined(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 the XNA FrameworkIsDefined(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 the XNA FrameworkSupported by Portable Class LibraryIsDefined(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 the XNA FrameworkIsDefined(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 the XNA FrameworkSupported by Portable Class LibraryIsDefined(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 the XNA FrameworkIsDefined(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 the XNA FrameworkSupported by Portable Class LibraryIsDefined(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 the XNA FrameworkMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object.
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers.
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface.
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1).
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an 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. For more information about using attributes, see Applying Attributes and Extending Metadata Using Attributes.

The following code example demonstrates the usage of Attribute.

Imports System
Imports System.Reflection

Public Module CustomAttrVB

    ' An enumeration of animals. Start at 1 (0 = uninitialized). 
    Public Enum Animal
        ' Pets
        Dog = 1
        Cat
        Bird
    End Enum 

    ' Visual Basic requires the AttributeUsage be specified. 
    ' A custom attribute to allow a target to have a pet.
    <AttributeUsage(AttributeTargets.Method)> _
    Public Class AnimalTypeAttribute
        Inherits Attribute

        ' The constructor is called when the attribute is set. 
        Public Sub New(ByVal animal As Animal)
            Me.thePet = animal
        End Sub 

        ' Keep a variable internally ... 
        Protected thePet As Animal

        ' .. and show a copy to the outside world. 
        Public Property Pet() As Animal
            Get 
                Return thePet
            End Get 
            Set(ByVal Value As Animal)
                thePet = Value
            End Set 
        End Property 

    End Class 

    ' A test class where each method has its own pet. 
    Class AnimalTypeTestClass

        <AnimalType(Animal.Dog)> _
        Public Sub DogMethod()
        End Sub

        <AnimalType(Animal.Cat)> _
        Public Sub CatMethod()
        End Sub

        <AnimalType(Animal.Bird)> _
        Public Sub BirdMethod()
        End Sub 
    End Class 

    ' The runtime test. 
    Sub Main()
        Dim testClass As New AnimalTypeTestClass()
        Dim tcType As Type = testClass.GetType()
        Dim mInfo As MethodInfo
        ' Iterate through all the methods of the class. 
        For Each mInfo In tcType.GetMethods()
            Dim attr As Attribute
            ' Iterate through all the attributes of the method. 
            For Each attr In Attribute.GetCustomAttributes(mInfo)
                If TypeOf attr Is AnimalTypeAttribute Then 
                    Dim attrCustom As AnimalTypeAttribute = _
                        CType(attr, AnimalTypeAttribute)
                    Console.WriteLine("Method {0} has a pet {1} attribute.", _
                         mInfo.Name(), attrCustom.Pet.ToString())
                End If 
            Next 
        Next 
    End Sub 
End Module 

' Output: 
' Method DogMethod has a pet Dog attribute. 
' Method CatMethod has a pet Cat attribute. 
' Method BirdMethod has a pet Bird attribute.

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

This type is thread safe.

System.Object
  System.Attribute
    System.AttributeUsageAttribute
    System.CLSCompliantAttribute
    System.ComponentModel.DefaultValueAttribute
    System.ComponentModel.EditorBrowsableAttribute
    System.Diagnostics.ConditionalAttribute
    System.Diagnostics.DebuggableAttribute
    System.FlagsAttribute
    System.MTAThreadAttribute
    System.ObsoleteAttribute
    System.ParamArrayAttribute
    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.AssemblyMetadataAttribute
    System.Reflection.AssemblyProductAttribute
    System.Reflection.AssemblySignatureKeyAttribute
    System.Reflection.AssemblyTitleAttribute
    System.Reflection.AssemblyTrademarkAttribute
    System.Reflection.AssemblyVersionAttribute
    System.Reflection.DefaultMemberAttribute
    System.Runtime.CompilerServices.AccessedThroughPropertyAttribute
    System.Runtime.CompilerServices.CallerFilePathAttribute
    System.Runtime.CompilerServices.CallerLineNumberAttribute
    System.Runtime.CompilerServices.CallerMemberNameAttribute
    System.Runtime.CompilerServices.CompilationRelaxationsAttribute
    System.Runtime.CompilerServices.CompilerGeneratedAttribute
    System.Runtime.CompilerServices.CustomConstantAttribute
    System.Runtime.CompilerServices.DecimalConstantAttribute
    System.Runtime.CompilerServices.ExtensionAttribute
    System.Runtime.CompilerServices.FixedBufferAttribute
    System.Runtime.CompilerServices.IndexerNameAttribute
    System.Runtime.CompilerServices.InternalsVisibleToAttribute
    System.Runtime.CompilerServices.MethodImplAttribute
    System.Runtime.CompilerServices.ReferenceAssemblyAttribute
    System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
    System.Runtime.CompilerServices.StateMachineAttribute
    System.Runtime.CompilerServices.TypeForwardedFromAttribute
    System.Runtime.CompilerServices.TypeForwardedToAttribute
    System.Runtime.CompilerServices.UnsafeValueTypeAttribute
    System.Runtime.InteropServices.ComVisibleAttribute
    System.Runtime.InteropServices.FieldOffsetAttribute
    System.Runtime.InteropServices.OutAttribute
    System.Runtime.InteropServices.StructLayoutAttribute
    System.Runtime.Versioning.TargetFrameworkAttribute
    System.Security.AllowPartiallyTrustedCallersAttribute
    System.Security.SecurityCriticalAttribute
    System.Security.SecuritySafeCriticalAttribute
    System.Security.SecurityTransparentAttribute
    System.STAThreadAttribute
    System.ThreadStaticAttribute
Show:
© 2014 Microsoft