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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取指定型別的自訂屬性,此屬性套用至指定組件。

C#
public static Attribute GetCustomAttribute (this System.Reflection.Assembly element, Type attributeType);
C#
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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取指定型別的自訂屬性,此屬性套用至指定成員。

C#
public static Attribute GetCustomAttribute (this System.Reflection.MemberInfo element, Type attributeType);
C#
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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取指定型別的自訂屬性,此屬性套用至指定模組。

C#
public static Attribute GetCustomAttribute (this System.Reflection.Module element, Type attributeType);
C#
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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取指定型別的自訂屬性,此屬性套用至指定參數。

C#
public static Attribute GetCustomAttribute (this System.Reflection.ParameterInfo element, Type attributeType);
C#
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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取只訂型別的自訂屬性,此屬性套用至指定成員,並且可選擇性檢查該成員的祖系。

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);

參數

element
MemberInfo

要檢查的成員。

attributeType
Type

要搜尋的屬性類型。

inherit
Boolean

true 表示檢查element的祖系,否則為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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取只訂型別的自訂屬性,此屬性套用至指定參數,並且可選擇性檢查該參數的祖系。

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);

參數

element
ParameterInfo

要檢查的參數。

attributeType
Type

要搜尋的屬性類型。

inherit
Boolean

true 表示檢查element的祖系,否則為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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取只訂型別的自訂屬性,此屬性套用至指定參數,並且可選擇性檢查該參數的祖系。

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;

類型參數

T

要搜尋的屬性類型。

參數

element
ParameterInfo

要檢查的參數。

inherit
Boolean

true 表示檢查element的祖系,否則為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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取只訂型別的自訂屬性,此屬性套用至指定成員,並且可選擇性檢查該成員的祖系。

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;

類型參數

T

要搜尋的屬性類型。

參數

element
MemberInfo

要檢查的成員。

inherit
Boolean

true 表示檢查element的祖系,否則為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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取指定型別的自訂屬性,此屬性套用至指定參數。

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;

類型參數

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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取指定型別的自訂屬性,此屬性套用至指定模組。

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;

類型參數

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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取指定型別的自訂屬性,此屬性套用至指定成員。

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;

類型參數

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)

來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs
來源:
CustomAttributeExtensions.cs

擷取指定型別的自訂屬性,此屬性套用至指定組件。

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;

類型參數

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