1. 程式人生 > 其它 >【C#反射】BindingFlags 列舉

【C#反射】BindingFlags 列舉

BindingFlags 列舉用途:Type的類方法中,用於篩選成員。

type.InvokeMember方法中

type.GetConstructor 方法中

type.GetFiles方法中

type.GetMethod方法中

type.GetNestedType方法中

type.GetMember方法中

[Flag]
public enum BindingFlags
{
Default = 0,
IgnoreCase = 1,
DeclaredOnly = 2,
Instance = 4,
static = 8,
Public = 16,
NonPublic = 32,
FlattenHierarchy 
= 64, InvokeMethod = 256, CreateInstance = 512, GetField = 1024, SetField = 2048, GetProperty = 4096, SetProperty = 8192, PutDispProperty = 16384, PutRefDispProperty = 32768, ExactBinding = 65536, SuppressChangeType = 131072, OptionalParamBinding = 262144, IgnoreReturn = 16777216, DoNotWrapExceptions = 33554432
}
【Default 】

【IgnoreCase = 1】 【DeclaredOnly = 2
BindingFlags.DeclaredOnly,僅搜尋 Type 上宣告的成員,而不搜尋被簡單繼承的成員。 【Instance = 4】 【Static = 8】 【Public = 16
指定 BindingFlags.Public 可在搜尋中包含公共成員。 【NonPublic = 32
指定 BindingFlags.NonPublic 可在搜尋中包含非公共成員(即私有成員和受保護的成員)。 【FlattenHierarchy = 64】 【InvokeMethod = 256】 【CreateInst ance = 512】 【GetField = 1024】 【SetField = 2048】 【GetProperty = 4096】 【setProperty = 8192】 【PutDispProperty = 16384】 【PutRefDispProperty = 32768】 【ExactBinding = 65536】 【SuppressChangeType = 131072】 【OptionalParamBinding = 262144】 【IgnoreReturn = 16777216】 【DoNotWrapExceptions = 33554432】

為了獲取返回值,必須指定 BindingFlags.Instance 或 BindingFlags.Static。

指定 BindingFlags.Public 可在搜尋中包含公共成員。

指定 BindingFlags.NonPublic 可在搜尋中包含非公共成員(即私有成員和受保護的成員)。

指定 BindingFlags.FlattenHierarchy 可包含層次結構上的靜態成員。

下列 BindingFlags 修飾符標誌可用於更改搜尋的執行方式:

BindingFlags.IgnoreCase,表示忽略 name 的大小寫。

BindingFlags.DeclaredOnly,僅搜尋 Type 上宣告的成員,而不搜尋被簡單繼承的成員。

可以使用下列 BindingFlags 呼叫標誌表示要對成員採取的操作:

CreateInstance,表示呼叫建構函式。忽略 name。對其他呼叫標誌無效。

InvokeMethod,表示呼叫方法,而不呼叫建構函式或型別初始值設定項。對 SetField 或 SetProperty 無效。

GetField,表示獲取欄位值。對 SetField 無效。

SetField,表示設定欄位值。對 GetField 無效。

GetProperty,表示獲取屬性。對 SetProperty 無效。

SetProperty 表示設定屬性。對 GetProperty 無效。

BindingFlags.Instance : 物件例項
BindingFlags.Static : 靜態成員
BindingFlags.Public : 指可在搜尋中包含公共成員
BindingFlags.NonPublic : 指可在搜尋中包含非公共成員(即私有成員和受保護的成員)
BindingFlags.FlattenHierarchy : 指可包含層次結構上的靜態成員
BindingFlags.IgnoreCase : 表示忽略 name 的大小寫
BindingFlags.DeclaredOnly : 僅搜尋 Type 上宣告的成員,而不搜尋被簡單繼承的成員
BindingFlags.CreateInstance : 表示呼叫建構函式。忽略 name。對其他呼叫標誌無效

程式設計是個人愛好