【问题标题】:C# Reflection - Get value of unknown field [duplicate]C#反射-获取未知字段的值[重复]
【发布时间】:2015-09-01 13:26:04
【问题描述】:

我正在尝试使用反射来调用带有一些参数的方法,但我在使用正确的设置时遇到了一些困难。这是我所做的。

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

【问题讨论】:

    标签: c# reflection


    【解决方案1】:

    Type.GetField(string) 只返回 public 字段。我怀疑你想要:

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

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多