CustomAttributeExtensions.GetCustomAttribute 方法

定义

检索应用于指定元素的自定义属性。

重载

GetCustomAttribute(Assembly, Type)

检索应用于指定程序集的指定类型的自定义特性。

GetCustomAttribute(MemberInfo, Type)

检索应用于指定成员的指定类型的自定义特性。

GetCustomAttribute(Module, Type)

检索应用于指定模块的指定类型的自定义特性。

GetCustomAttribute(ParameterInfo, Type)

检索应用于指定参数的指定类型的自定义特性。

GetCustomAttribute(MemberInfo, Type, Boolean)

检索应用于指定成员的指定类型的自定义特性,并可选择检查该成员的上级。

GetCustomAttribute(ParameterInfo, Type, Boolean)

检索应用于指定参数的指定类型的自定义特性,并可选择检查该参数的上级。

GetCustomAttribute<T>(ParameterInfo, Boolean)

检索应用于指定参数的指定类型的自定义特性,并可选择检查该参数的上级。

GetCustomAttribute<T>(MemberInfo, Boolean)

检索应用于指定成员的指定类型的自定义特性,并可选择检查该成员的上级。

GetCustomAttribute<T>(ParameterInfo)

检索应用于指定参数的指定类型的自定义特性。

GetCustomAttribute<T>(Module)

检索应用于指定模块的指定类型的自定义特性。

GetCustomAttribute<T>(MemberInfo)

检索应用于指定成员的指定类型的自定义特性。

GetCustomAttribute<T>(Assembly)

检索应用于指定程序集的指定类型的自定义特性。

GetCustomAttribute(Assembly, Type)

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

检索应用于指定程序集的指定类型的自定义特性。

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

参数

element
Assembly

要检查的程序集。

attributeType
Type

要搜索的属性的类型。

返回

attributeType 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementattributeTypenull

attributeType 不是从 Attribute 派生的。

找到多个请求的属性。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定成员的指定类型的自定义特性。

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

参数

element
MemberInfo

要检查的成员。

attributeType
Type

要搜索的属性的类型。

返回

attributeType 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementattributeTypenull

attributeType 不是从 Attribute 派生的。

element 不是构造函数、方法、属性、事件、类型或字段。

找到多个请求的属性。

无法加载自定义属性类型。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定模块的指定类型的自定义特性。

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

参数

element
Module

要检查的模块。

attributeType
Type

要搜索的属性的类型。

返回

attributeType 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementattributeTypenull

attributeType 不是从 Attribute 派生的。

找到多个请求的属性。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定参数的指定类型的自定义特性。

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

参数

element
ParameterInfo

要检查的参数。

attributeType
Type

要搜索的属性的类型。

返回

attributeType 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementattributeTypenull

attributeType 不是从 Attribute 派生的。

找到多个请求的属性。

无法加载自定义属性类型。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定成员的指定类型的自定义特性,并可选择检查该成员的上级。

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

参数

element
MemberInfo

要检查的成员。

attributeType
Type

要搜索的属性的类型。

inherit
Boolean

如果检查 element 的上级,则为 true;否则为 false

返回

attributeType 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementattributeTypenull

attributeType 不是从 Attribute 派生的。

element 不是构造函数、方法、属性、事件、类型或字段。

找到多个请求的属性。

无法加载自定义属性类型。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定参数的指定类型的自定义特性,并可选择检查该参数的上级。

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

参数

element
ParameterInfo

要检查的参数。

attributeType
Type

要搜索的属性的类型。

inherit
Boolean

如果检查 element 的上级,则为 true;否则为 false

返回

匹配 attributeType 的自定义特性,如果未找到此类特性,则为 null

例外

elementattributeTypenull

attributeType 不是从 Attribute 派生的。

找到多个请求的属性。

无法加载自定义属性类型。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定参数的指定类型的自定义特性,并可选择检查该参数的上级。

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

类型参数

T

要搜索的属性的类型。

参数

element
ParameterInfo

要检查的参数。

inherit
Boolean

如果检查 element 的上级,则为 true;否则为 false

返回

T

T 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementnull

element 不是构造函数、方法、属性、事件、类型或字段。

找到多个请求的属性。

无法加载自定义属性类型。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定成员的指定类型的自定义特性,并可选择检查该成员的上级。

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

类型参数

T

要搜索的属性的类型。

参数

element
MemberInfo

要检查的成员。

inherit
Boolean

如果检查 element 的上级,则为 true;否则为 false

返回

T

T 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementnull

element 不是构造函数、方法、属性、事件、类型或字段。

找到多个请求的属性。

无法加载自定义属性类型。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定参数的指定类型的自定义特性。

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

类型参数

T

要搜索的属性的类型。

参数

element
ParameterInfo

要检查的参数。

返回

T

T 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementnull

element 不是构造函数、方法、属性、事件、类型或字段。

找到多个请求的属性。

无法加载自定义属性类型。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定模块的指定类型的自定义特性。

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

类型参数

T

要搜索的属性的类型。

参数

element
Module

要检查的模块。

返回

T

T 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementnull

找到多个请求的属性。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定成员的指定类型的自定义特性。

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

类型参数

T

要搜索的属性的类型。

参数

element
MemberInfo

要检查的成员。

返回

T

T 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementnull

element 不是构造函数、方法、属性、事件、类型或字段。

找到多个请求的属性。

无法加载自定义属性类型。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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

检索应用于指定程序集的指定类型的自定义特性。

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

类型参数

T

要搜索的属性的类型。

参数

element
Assembly

要检查的程序集。

返回

T

T 匹配的自定义特性,如果未找到此类特性,则为 null

例外

elementnull

找到多个请求的属性。

注解

GetCustomAttributes如果希望返回或将引发多个值,AmbiguousMatchException请使用扩展方法。

适用于

.NET 9 和其他版本
产品 版本
.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