CustomAttributeData Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides access to custom attribute data for assemblies, modules, types, members and parameters that are loaded into the reflection-only context.

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


type CustomAttributeData = class end


Initializes a new instance of the CustomAttributeData class.


Gets the type of the attribute.


Gets a ConstructorInfo object that represents the constructor that would have initialized the custom attribute.


Gets the list of positional arguments specified for the attribute instance represented by the CustomAttributeData object.


Gets the list of named arguments specified for the attribute instance represented by the CustomAttributeData object.


Returns a value that indicates whether this instance is equal to a specified object.(Overrides Object.Equals(Object).)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target assembly.


Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.


Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target module.


Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target parameter.


Serves as a hash function for a particular type.(Overrides Object.GetHashCode().)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string representation of the custom attribute.(Overrides Object.ToString().)

Code that is being examined in the reflection-only context cannot be executed, so it is not always possible to examine custom attributes by creating instances of them and then examining their properties, using methods like Attribute.GetCustomAttributes, MemberInfo.GetCustomAttributes, and so on. If the code for the attribute type itself is loaded into the reflection-only context, it cannot be executed.

The CustomAttributeData class allows examination of custom attributes in the reflection-only context by providing an abstraction for attributes. The members of this class can be used to obtain the positional arguments and named arguments of the attribute. Use the ConstructorArguments property to get a list of CustomAttributeTypedArgument structures that represent the positional arguments, and use the NamedArguments property to get a list of CustomAttributeNamedArgument structures that represent the named arguments.


The CustomAttributeNamedArgument structure only provides information about the attribute property used to get and set the argument value. To obtain the type and value of the argument, use the CustomAttributeNamedArgument.TypedValue property to obtain a CustomAttributeTypedArgument structure.

When you have a CustomAttributeTypedArgument structure for an argument, whether named or positional, use the CustomAttributeTypedArgument.ArgumentType property to get the type and the CustomAttributeTypedArgument.Value property to get the value.


For an array argument, the CustomAttributeTypedArgument.Value property returns a generic ReadOnlyCollection<'T> of CustomAttributeTypedArgument objects. Each CustomAttributeTypedArgument object in the collection represents the corresponding element of the array.

CustomAttributeData can be used in the execution context as well as in the reflection-only context. For example, you might want to avoid loading the assembly that contains the code for a custom attribute. Using the CustomAttributeData class is different from using methods like Attribute.GetCustomAttributes:

  • The properties and methods of CustomAttributeData only provide you with the values that were specified for the attribute instance, not the semantics of the constructor. For example, a string argument of an attribute might be converted internally to some other representation, and returned in a canonical form; or a property might have side effects when the actual attribute code is executed.

  • The properties and methods of CustomAttributeData do not allow you to retrieve the custom attributes inherited from base classes.

To create instances of the CustomAttributeData class, use the static (Shared in Visual Basic) GetCustomAttributes factory methods.

The following example defines a custom attribute with four constructors and four properties. Two of the properties are read-only, and are set by using the positional parameters of the constructors. The other two properties are read/write, and can be set only by using named arguments. One positional property is an array of strings, and one named property is an array of integers.

The attribute is applied to the assembly, to a type declared in the assembly, to a method of the type, and to a parameter of the method. Different constructors are used for these cases. When executed, the assembly loads itself into the reflection-only context and displays information about the custom attributes that were applied to it and to the type and members it contains.

The attribute that is applied to the type demonstrates array properties, with both positional and named arguments.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top