我試圖getField,但總是返回null 下面是代碼和變量的監視圖像。GetField返回null
代碼:FieldInfo xSortField = xFieldInfo.GetValue(x).GetType().GetField(this.prefixedSortBy[i]);
我試圖getField,但總是返回null 下面是代碼和變量的監視圖像。GetField返回null
代碼:FieldInfo xSortField = xFieldInfo.GetValue(x).GetType().GetField(this.prefixedSortBy[i]);
我認爲你需要在你在做什麼更密切地關注,因爲似乎沒什麼道理不。
xFieldInfo.GetValue(x)
返回一個盒裝整數2
。 GetType()
然後返回typeof(int)
你再試圖獲得FieldInfo
田間ssId
上int
。這不存在。
看起來你打算在:
FieldInfo xSortField = x.GetType().GetField(this.prefixedSortBy[i])
是的,它工作,謝謝查爾斯 – Luis
您必須得到一個返回值,請指定BindingFlags.Instance
或BindingFlags.Static
作爲第二個參數。
還應該使用BindingFlags.NonPublic
來獲取非公有領域。
參見MSDN以供參考。
您需要使用的BindingFlags
GetField("FieldName", BindingFlags.Instance | BindingFlags.Public);
嘗試增加綁定標誌實例的getfield命令調用:'getfield命令(this.prefixedSortBy [I],BindingFlags.Instance)' – Christian
你正在試圖獲得場'Int32'上的'ssId'?這不存在。 –