CustomAttributeExtensions.GetCustomAttribute Method

Definition

Retrieves a custom attribute that is applied to a specified element.

Overloads

GetCustomAttribute(Assembly, Type)

Retrieves a custom attribute of a specified type that is applied to a specified assembly.

GetCustomAttribute(MemberInfo, Type)

Retrieves a custom attribute of a specified type that is applied to a specified member.

GetCustomAttribute(Module, Type)

Retrieves a custom attribute of a specified type that is applied to a specified module.

GetCustomAttribute(ParameterInfo, Type)

Retrieves a custom attribute of a specified type that is applied to a specified parameter.

GetCustomAttribute(MemberInfo, Type, Boolean)

Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttribute(ParameterInfo, Type, Boolean)

Retrieves a custom attribute of a specified type that is applied to a specified parameter, and optionally inspects the ancestors of that parameter.

GetCustomAttribute<T>(ParameterInfo, Boolean)

Retrieves a custom attribute of a specified type that is applied to a specified parameter, and optionally inspects the ancestors of that parameter.

GetCustomAttribute<T>(MemberInfo, Boolean)

Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.

GetCustomAttribute<T>(ParameterInfo)

Retrieves a custom attribute of a specified type that is applied to a specified parameter.

GetCustomAttribute<T>(Module)

Retrieves a custom attribute of a specified type that is applied to a specified module.

GetCustomAttribute<T>(MemberInfo)

Retrieves a custom attribute of a specified type that is applied to a specified member.

GetCustomAttribute<T>(Assembly)

Retrieves a custom attribute of a specified type that is applied to a specified assembly.

GetCustomAttribute(Assembly, Type)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified assembly.

C#
public static Attribute GetCustomAttribute (this System.Reflection.Assembly element, Type attributeType);
C#
public static Attribute? GetCustomAttribute (this System.Reflection.Assembly element, Type attributeType);

Parameters

element
Assembly

The assembly to inspect.

attributeType
Type

The type of attribute to search for.

Returns

A custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

element or attributeType is null.

attributeType is not derived from Attribute.

More than one of the requested attributes was found.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute(MemberInfo, Type)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified member.

C#
public static Attribute GetCustomAttribute (this System.Reflection.MemberInfo element, Type attributeType);
C#
public static Attribute? GetCustomAttribute (this System.Reflection.MemberInfo element, Type attributeType);

Parameters

element
MemberInfo

The member to inspect.

attributeType
Type

The type of attribute to search for.

Returns

A custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

element or attributeType is null.

attributeType is not derived from Attribute.

element is not a constructor, method, property, event, type, or field.

More than one of the requested attributes was found.

A custom attribute type cannot be loaded.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute(Module, Type)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified module.

C#
public static Attribute GetCustomAttribute (this System.Reflection.Module element, Type attributeType);
C#
public static Attribute? GetCustomAttribute (this System.Reflection.Module element, Type attributeType);

Parameters

element
Module

The module to inspect.

attributeType
Type

The type of attribute to search for.

Returns

A custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

element or attributeType is null.

attributeType is not derived from Attribute.

More than one of the requested attributes was found.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute(ParameterInfo, Type)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified parameter.

C#
public static Attribute GetCustomAttribute (this System.Reflection.ParameterInfo element, Type attributeType);
C#
public static Attribute? GetCustomAttribute (this System.Reflection.ParameterInfo element, Type attributeType);

Parameters

element
ParameterInfo

The parameter to inspect.

attributeType
Type

The type of attribute to search for.

Returns

A custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

element or attributeType is null.

attributeType is not derived from Attribute.

More than one of the requested attributes was found.

A custom attribute type cannot be loaded.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute(MemberInfo, Type, Boolean)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.

C#
public static Attribute GetCustomAttribute (this System.Reflection.MemberInfo element, Type attributeType, bool inherit);
C#
public static Attribute? GetCustomAttribute (this System.Reflection.MemberInfo element, Type attributeType, bool inherit);

Parameters

element
MemberInfo

The member to inspect.

attributeType
Type

The type of attribute to search for.

inherit
Boolean

true to inspect the ancestors of element; otherwise, false.

Returns

A custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

element or attributeType is null.

attributeType is not derived from Attribute.

element is not a constructor, method, property, event, type, or field.

More than one of the requested attributes was found.

A custom attribute type cannot be loaded.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute(ParameterInfo, Type, Boolean)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified parameter, and optionally inspects the ancestors of that parameter.

C#
public static Attribute GetCustomAttribute (this System.Reflection.ParameterInfo element, Type attributeType, bool inherit);
C#
public static Attribute? GetCustomAttribute (this System.Reflection.ParameterInfo element, Type attributeType, bool inherit);

Parameters

element
ParameterInfo

The parameter to inspect.

attributeType
Type

The type of attribute to search for.

inherit
Boolean

true to inspect the ancestors of element; otherwise, false.

Returns

A custom attribute matching attributeType, or null if no such attribute is found.

Exceptions

element or attributeType is null.

attributeType is not derived from Attribute.

More than one of the requested attributes was found.

A custom attribute type cannot be loaded.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute<T>(ParameterInfo, Boolean)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified parameter, and optionally inspects the ancestors of that parameter.

C#
public static T GetCustomAttribute<T> (this System.Reflection.ParameterInfo element, bool inherit) where T : Attribute;
C#
public static T? GetCustomAttribute<T> (this System.Reflection.ParameterInfo element, bool inherit) where T : Attribute;

Type Parameters

T

The type of attribute to search for.

Parameters

element
ParameterInfo

The parameter to inspect.

inherit
Boolean

true to inspect the ancestors of element; otherwise, false.

Returns

T

A custom attribute that matches T, or null if no such attribute is found.

Exceptions

element is null.

element is not a constructor, method, property, event, type, or field.

More than one of the requested attributes was found.

A custom attribute type cannot be loaded.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute<T>(MemberInfo, Boolean)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.

C#
public static T GetCustomAttribute<T> (this System.Reflection.MemberInfo element, bool inherit) where T : Attribute;
C#
public static T? GetCustomAttribute<T> (this System.Reflection.MemberInfo element, bool inherit) where T : Attribute;

Type Parameters

T

The type of attribute to search for.

Parameters

element
MemberInfo

The member to inspect.

inherit
Boolean

true to inspect the ancestors of element; otherwise, false.

Returns

T

A custom attribute that matches T, or null if no such attribute is found.

Exceptions

element is null.

element is not a constructor, method, property, event, type, or field.

More than one of the requested attributes was found.

A custom attribute type cannot be loaded.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute<T>(ParameterInfo)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified parameter.

C#
public static T GetCustomAttribute<T> (this System.Reflection.ParameterInfo element) where T : Attribute;
C#
public static T? GetCustomAttribute<T> (this System.Reflection.ParameterInfo element) where T : Attribute;

Type Parameters

T

The type of attribute to search for.

Parameters

element
ParameterInfo

The parameter to inspect.

Returns

T

A custom attribute that matches T, or null if no such attribute is found.

Exceptions

element is null.

element is not a constructor, method, property, event, type, or field.

More than one of the requested attributes was found.

A custom attribute type cannot be loaded.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute<T>(Module)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified module.

C#
public static T GetCustomAttribute<T> (this System.Reflection.Module element) where T : Attribute;
C#
public static T? GetCustomAttribute<T> (this System.Reflection.Module element) where T : Attribute;

Type Parameters

T

The type of attribute to search for.

Parameters

element
Module

The module to inspect.

Returns

T

A custom attribute that matches T, or null if no such attribute is found.

Exceptions

element is null.

More than one of the requested attributes was found.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute<T>(MemberInfo)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified member.

C#
public static T GetCustomAttribute<T> (this System.Reflection.MemberInfo element) where T : Attribute;
C#
public static T? GetCustomAttribute<T> (this System.Reflection.MemberInfo element) where T : Attribute;

Type Parameters

T

The type of attribute to search for.

Parameters

element
MemberInfo

The member to inspect.

Returns

T

A custom attribute that matches T, or null if no such attribute is found.

Exceptions

element is null.

element is not a constructor, method, property, event, type, or field.

More than one of the requested attributes was found.

A custom attribute type cannot be loaded.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetCustomAttribute<T>(Assembly)

Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs
Source:
CustomAttributeExtensions.cs

Retrieves a custom attribute of a specified type that is applied to a specified assembly.

C#
public static T GetCustomAttribute<T> (this System.Reflection.Assembly element) where T : Attribute;
C#
public static T? GetCustomAttribute<T> (this System.Reflection.Assembly element) where T : Attribute;

Type Parameters

T

The type of attribute to search for.

Parameters

element
Assembly

The assembly to inspect.

Returns

T

A custom attribute that matches T, or null if no such attribute is found.

Exceptions

element is null.

More than one of the requested attributes was found.

Remarks

Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0