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)

检索应用于指定程序集的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified assembly.

public:
[System::Runtime::CompilerServices::Extension]
 static Attribute ^ GetCustomAttribute(System::Reflection::Assembly ^ element, Type ^ attributeType);
public static Attribute GetCustomAttribute (this System.Reflection.Assembly element, Type attributeType);
static member GetCustomAttribute : System.Reflection.Assembly * Type -> Attribute
<Extension()>
Public Function GetCustomAttribute (element As Assembly, attributeType As Type) As Attribute

Parameters

element
Assembly

要检查的程序集。The assembly to inspect.

attributeType
Type

要搜索的特性的类型。The type of attribute to search for.

Returns

Attribute

attributeType 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

elementattributeTypenullelement or attributeType is null.

attributeType 不是从 Attribute 派生的。attributeType is not derived from Attribute.

找到多个请求的属性。More than one of the requested attributes was found.

Remarks

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

GetCustomAttribute(MemberInfo, Type)

检索应用于指定成员的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified member.

public:
[System::Runtime::CompilerServices::Extension]
 static Attribute ^ GetCustomAttribute(System::Reflection::MemberInfo ^ element, Type ^ attributeType);
public static Attribute GetCustomAttribute (this System.Reflection.MemberInfo element, Type attributeType);
static member GetCustomAttribute : System.Reflection.MemberInfo * Type -> Attribute
<Extension()>
Public Function GetCustomAttribute (element As MemberInfo, attributeType As Type) As Attribute

Parameters

element
MemberInfo

要检查的成员。The member to inspect.

attributeType
Type

要搜索的特性的类型。The type of attribute to search for.

Returns

Attribute

attributeType 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

elementattributeTypenullelement or attributeType is null.

attributeType 不是从 Attribute 派生的。attributeType is not derived from Attribute.

element 不是构造函数、方法、属性、事件、类型或字段。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

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

GetCustomAttribute(Module, Type)

检索应用于指定模块的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified module.

public:
[System::Runtime::CompilerServices::Extension]
 static Attribute ^ GetCustomAttribute(System::Reflection::Module ^ element, Type ^ attributeType);
public static Attribute GetCustomAttribute (this System.Reflection.Module element, Type attributeType);
static member GetCustomAttribute : System.Reflection.Module * Type -> Attribute
<Extension()>
Public Function GetCustomAttribute (element As Module, attributeType As Type) As Attribute

Parameters

element
Module

要检查的模块。The module to inspect.

attributeType
Type

要搜索的特性的类型。The type of attribute to search for.

Returns

Attribute

attributeType 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

elementattributeTypenullelement or attributeType is null.

attributeType 不是从 Attribute 派生的。attributeType is not derived from Attribute.

找到多个请求的属性。More than one of the requested attributes was found.

Remarks

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

GetCustomAttribute(ParameterInfo, Type)

检索应用于指定参数的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified parameter.

public:
[System::Runtime::CompilerServices::Extension]
 static Attribute ^ GetCustomAttribute(System::Reflection::ParameterInfo ^ element, Type ^ attributeType);
public static Attribute GetCustomAttribute (this System.Reflection.ParameterInfo element, Type attributeType);
static member GetCustomAttribute : System.Reflection.ParameterInfo * Type -> Attribute
<Extension()>
Public Function GetCustomAttribute (element As ParameterInfo, attributeType As Type) As Attribute

Parameters

element
ParameterInfo

要检查的参数。The parameter to inspect.

attributeType
Type

要搜索的特性的类型。The type of attribute to search for.

Returns

Attribute

attributeType 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

elementattributeTypenullelement or attributeType is null.

attributeType 不是从 Attribute 派生的。attributeType is not derived from Attribute.

找到多个请求的属性。More than one of the requested attributes was found.

无法加载自定义属性类型。A custom attribute type cannot be loaded.

Remarks

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

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.

public:
[System::Runtime::CompilerServices::Extension]
 static Attribute ^ GetCustomAttribute(System::Reflection::MemberInfo ^ element, Type ^ attributeType, bool inherit);
public static Attribute GetCustomAttribute (this System.Reflection.MemberInfo element, Type attributeType, bool inherit);
static member GetCustomAttribute : System.Reflection.MemberInfo * Type * bool -> Attribute
<Extension()>
Public Function GetCustomAttribute (element As MemberInfo, attributeType As Type, inherit As Boolean) As Attribute

Parameters

element
MemberInfo

要检查的成员。The member to inspect.

attributeType
Type

要搜索的特性的类型。The type of attribute to search for.

inherit
Boolean

如果检查 element 的上级,则为 true;否则为 falsetrue to inspect the ancestors of element; otherwise, false.

Returns

Attribute

attributeType 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches attributeType, or null if no such attribute is found.

Exceptions

elementattributeTypenullelement or attributeType is null.

attributeType 不是从 Attribute 派生的。attributeType is not derived from Attribute.

element 不是构造函数、方法、属性、事件、类型或字段。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

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

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.

public:
[System::Runtime::CompilerServices::Extension]
 static Attribute ^ GetCustomAttribute(System::Reflection::ParameterInfo ^ element, Type ^ attributeType, bool inherit);
public static Attribute GetCustomAttribute (this System.Reflection.ParameterInfo element, Type attributeType, bool inherit);
static member GetCustomAttribute : System.Reflection.ParameterInfo * Type * bool -> Attribute
<Extension()>
Public Function GetCustomAttribute (element As ParameterInfo, attributeType As Type, inherit As Boolean) As Attribute

Parameters

element
ParameterInfo

要检查的参数。The parameter to inspect.

attributeType
Type

要搜索的特性的类型。The type of attribute to search for.

inherit
Boolean

如果检查 element 的上级,则为 true;否则为 falsetrue to inspect the ancestors of element; otherwise, false.

Returns

Attribute

匹配 attributeType 的自定义特性,如果未找到此类特性,则为 nullA custom attribute matching attributeType, or null if no such attribute is found.

Exceptions

elementattributeTypenullelement or attributeType is null.

attributeType 不是从 Attribute 派生的。attributeType is not derived from Attribute.

找到多个请求的属性。More than one of the requested attributes was found.

无法加载自定义属性类型。A custom attribute type cannot be loaded.

Remarks

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

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.

public:
generic <typename T>
 where T : Attribute[System::Runtime::CompilerServices::Extension]
 static T GetCustomAttribute(System::Reflection::ParameterInfo ^ element, bool inherit);
public static T GetCustomAttribute<T> (this System.Reflection.ParameterInfo element, bool inherit) where T : Attribute;
static member GetCustomAttribute : System.Reflection.ParameterInfo * bool -> 'T (requires 'T :> Attribute)
<Extension()>
Public Function GetCustomAttribute(Of T As Attribute) (element As ParameterInfo, inherit As Boolean) As T

Type Parameters

T

要搜索的特性的类型。The type of attribute to search for.

Parameters

element
ParameterInfo

要检查的参数。The parameter to inspect.

inherit
Boolean

如果检查 element 的上级,则为 true;否则为 falsetrue to inspect the ancestors of element; otherwise, false.

Returns

T

T 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches T, or null if no such attribute is found.

Exceptions

elementnullelement is null.

element 不是构造函数、方法、属性、事件、类型或字段。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

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

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.

public:
generic <typename T>
 where T : Attribute[System::Runtime::CompilerServices::Extension]
 static T GetCustomAttribute(System::Reflection::MemberInfo ^ element, bool inherit);
public static T GetCustomAttribute<T> (this System.Reflection.MemberInfo element, bool inherit) where T : Attribute;
static member GetCustomAttribute : System.Reflection.MemberInfo * bool -> 'T (requires 'T :> Attribute)
<Extension()>
Public Function GetCustomAttribute(Of T As Attribute) (element As MemberInfo, inherit As Boolean) As T

Type Parameters

T

要搜索的特性的类型。The type of attribute to search for.

Parameters

element
MemberInfo

要检查的成员。The member to inspect.

inherit
Boolean

如果检查 element 的上级,则为 true;否则为 falsetrue to inspect the ancestors of element; otherwise, false.

Returns

T

T 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches T, or null if no such attribute is found.

Exceptions

elementnullelement is null.

element 不是构造函数、方法、属性、事件、类型或字段。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

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

GetCustomAttribute<T>(ParameterInfo)

检索应用于指定参数的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified parameter.

public:
generic <typename T>
 where T : Attribute[System::Runtime::CompilerServices::Extension]
 static T GetCustomAttribute(System::Reflection::ParameterInfo ^ element);
public static T GetCustomAttribute<T> (this System.Reflection.ParameterInfo element) where T : Attribute;
static member GetCustomAttribute : System.Reflection.ParameterInfo -> 'T (requires 'T :> Attribute)
<Extension()>
Public Function GetCustomAttribute(Of T As Attribute) (element As ParameterInfo) As T

Type Parameters

T

要搜索的特性的类型。The type of attribute to search for.

Parameters

element
ParameterInfo

要检查的参数。The parameter to inspect.

Returns

T

T 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches T, or null if no such attribute is found.

Exceptions

elementnullelement is null.

element 不是构造函数、方法、属性、事件、类型或字段。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

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

GetCustomAttribute<T>(Module)

检索应用于指定模块的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified module.

public:
generic <typename T>
 where T : Attribute[System::Runtime::CompilerServices::Extension]
 static T GetCustomAttribute(System::Reflection::Module ^ element);
public static T GetCustomAttribute<T> (this System.Reflection.Module element) where T : Attribute;
static member GetCustomAttribute : System.Reflection.Module -> 'T (requires 'T :> Attribute)
<Extension()>
Public Function GetCustomAttribute(Of T As Attribute) (element As Module) As T

Type Parameters

T

要搜索的特性的类型。The type of attribute to search for.

Parameters

element
Module

要检查的模块。The module to inspect.

Returns

T

T 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches T, or null if no such attribute is found.

Exceptions

elementnullelement is null.

找到多个请求的属性。More than one of the requested attributes was found.

Remarks

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

GetCustomAttribute<T>(MemberInfo)

检索应用于指定成员的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified member.

public:
generic <typename T>
 where T : Attribute[System::Runtime::CompilerServices::Extension]
 static T GetCustomAttribute(System::Reflection::MemberInfo ^ element);
public static T GetCustomAttribute<T> (this System.Reflection.MemberInfo element) where T : Attribute;
static member GetCustomAttribute : System.Reflection.MemberInfo -> 'T (requires 'T :> Attribute)
<Extension()>
Public Function GetCustomAttribute(Of T As Attribute) (element As MemberInfo) As T

Type Parameters

T

要搜索的特性的类型。The type of attribute to search for.

Parameters

element
MemberInfo

要检查的成员。The member to inspect.

Returns

T

T 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches T, or null if no such attribute is found.

Exceptions

elementnullelement is null.

element 不是构造函数、方法、属性、事件、类型或字段。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

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

GetCustomAttribute<T>(Assembly)

检索应用于指定程序集的指定类型的自定义特性。Retrieves a custom attribute of a specified type that is applied to a specified assembly.

public:
generic <typename T>
 where T : Attribute[System::Runtime::CompilerServices::Extension]
 static T GetCustomAttribute(System::Reflection::Assembly ^ element);
public static T GetCustomAttribute<T> (this System.Reflection.Assembly element) where T : Attribute;
static member GetCustomAttribute : System.Reflection.Assembly -> 'T (requires 'T :> Attribute)
<Extension()>
Public Function GetCustomAttribute(Of T As Attribute) (element As Assembly) As T

Type Parameters

T

要搜索的特性的类型。The type of attribute to search for.

Parameters

element
Assembly

要检查的程序集。The assembly to inspect.

Returns

T

T 匹配的自定义特性,如果未找到此类特性,则为 nullA custom attribute that matches T, or null if no such attribute is found.

Exceptions

elementnullelement is null.

找到多个请求的属性。More than one of the requested attributes was found.

Remarks

如果希望返回多个值或将引发 AmbiguousMatchException,请使用 GetCustomAttributes 扩展方法。Use the GetCustomAttributes extension method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

Applies to