2015-09-01 2 views
4

Я пытаюсь использовать отражение для вызова метода с некоторыми аргументами, но у меня есть некоторые трудности с использованием правильной настройки. Вот что я сделал.C# Reflection - получить значение неизвестного поля

// The method I am trying to call 
// UnityGUI 
// internal static float DoFloatField(EditorGUI.RecycledTextEditor editor, Rect position, Rect dragHotZone, int id, float value, string formatString, GUIStyle style, bool draggable) 
// Called this way : EditorGUI.DoFloatField(EditorGUI.s_RecycledEditor, position, new Rect(0f, 0f, 0f, 0f), controlID, value, EditorGUI.kFloatFieldFormatString, style, false); 

Type editorGUIType = typeof(EditorGUI); 
Type RecycledTextEditorType = Assembly.GetAssembly(editorGUIType).GetType("UnityEditor.EditorGUI+RecycledTextEditor"); 
Type[] argumentTypes = new Type[] { RecycledTextEditorType, typeof(Rect), typeof(Rect), typeof(int), typeof(float), typeof(string), typeof(GUIStyle), typeof(bool) }; 
MethodInfo doFloatFieldMethod = editorGUIType.GetMethod("DoFloatField", BindingFlags.NonPublic | BindingFlags.Static, null, argumentTypes, null); 

// Here is the invoke part but I don't know how I can access to the s_RecycledEditor variable of type EditorGUI.RecycledTextEditor 

FieldInfo fieldInfo = editorGUIType.GetField("s_RecycledEditor"); // This is null... 
object o = fieldInfo.GetValue(null); // ... So the next part can't work. 
object[] parameters = new object[]{ o, position2, position, controlID, controlID, "g7", style, true }; 
doFloatFieldMethod.Invoke(null, parameters); 

Я не знаю, как я могу получить значение s_RecycledEditor и если рядом кода работает. Я также не знаю, следует ли использовать PropertyInfo или FieldInfo.

спасибо.

Редактировать: Я забыл добавить описание s_RecycledTextEditor.

internal static EditorGUI.RecycledTextEditor s_RecycledEditor = new EditorGUI.RecycledTextEditor(); 

ответ

9

Type.GetField(string) возвращает только общественные поля. Я думаю, вы хотите:

FieldInfo fieldInfo = editorGUIType.GetField(
    "s_RecycledEditor", BindingFlags.NonPublic | BindingFlags.Static); 
+0

Большое спасибо @ Jon Skeet! – MaT

Смежные вопросы