我正在使用C#反射這裏: 我有一個屬性的FieldInfo,我想獲取它所屬的類的實例(所以我可以達到另一個屬性的內容):從FieldInfo獲取容器類實例
對於爲例藉此類:
class MyClass
{
public int A { get; set; }
public int B { get; set; }
}
在代碼中的某些部分我有
void Function(FieldInfo fieldInfoOfA)
{
// here I need to find the value of B
}
這可能嗎?
在範圍內有fieldInfoOfA的同時,您是否有範圍內的MyClass對象? – MatthewMartin
我只有這個FieldInfo來自外部代碼。要添加一些上下文,我正在使用Unity的PropertyDrawer(http://docs.unity3d.com/ScriptReference/PropertyDrawer-fieldInfo.html),但它不相關。 – Titan
如果您需要通過反射字段的值,請查看此問題:https://stackoverflow.com/questions/6961781/reflecting-a-private-field-from-a-base-class?rq=1 – MatthewMartin