C# 反射不调用方法

标签 c# reflection invoke

我正在使用带有反射的 Unity,并且我尝试调用某个方法名称 Start,但我的代码没有调用它

这是 ModLoader.cs:

using UnityEngine;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Collections.Generic;

public class ModLoader : MonoBehaviour {
    List<MethodInfo> modMethods = new List<MethodInfo>();

    // Use this for initialization
    void Start () {
        if (!Directory.Exists (Application.dataPath + "/../Mods")) {
            Directory.CreateDirectory (Application.dataPath + "/../Mods");
        }

        foreach (var mod in Directory.GetFiles(Application.dataPath + "/../Mods", "*.dll")) {
            var assembly = Assembly.LoadFile(mod);
            foreach (var type in assembly.GetTypes()) {
                foreach (var method in type.GetMethods()) {
                    modMethods.Add (method);
                }
            }
        }

        //Execute Start method in all mods
        foreach (MethodInfo method in modMethods) {
            print (method.Name);
            if (method.Name == "Start" && method.GetParameters().Length == 0 && method.IsStatic) {
                method.Invoke (null, new object[]{  });
            }
        }
    }

    // Update is called once per frame
    void Update () {
        //Execute Update method in all mods
        foreach (MethodInfo method in modMethods) {
            if (method.Name == "Update" && method.GetParameters().Length == 0 && method.IsStatic) {
                method.Invoke (null, new object[]{  });
            }
        }
    }
}

这是我的模组(.dll):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class Class1 {
    static void Start () {
        Debug.Log("hello world");

        foreach (GameObject go in GameObject.FindObjectsOfType<GameObject>()) {
            if (go.GetComponent<MeshRenderer>()) {
                go.GetComponent<MeshRenderer>().material.color = new Color(1f, 0f, 0f);
            }
        }
    }
}

我在“Mods”文件夹中拥有来自构建的 dll,并且我知道我的脚本找到了它,我只是不知道为什么没有调用方法 id。

最佳答案

模块中的 Start 方法是私有(private)的。默认情况下,反射适用于公共(public)方法。您需要将其公开:

public class Class1 {
    public static void Start () {
        Debug.Log("hello world");

        foreach (GameObject go in GameObject.FindObjectsOfType<GameObject>()) {
            if (go.GetComponent<MeshRenderer>()) {
                go.GetComponent<MeshRenderer>().material.color = new Color(1f, 0f, 0f);
            }
        }
    }
}

或指定 BindingFlags对于 GetMethods 方法。您需要 BindingFlags.Static 和 BindingFlags.NonPublic

type.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)

关于C# 反射不调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37311059/

相关文章:

java - 嵌套类中的反射

Java - 调用异常

C# - 从工作线程而不是主线程访问时,类字段为空

c# - 您可以直接使用 "CALL Proc_name()"作为字符串,或者在 C# 中使用 CommandType.StoredProcedure

c# - Linq Contains() 是否检查 HashSet?

C# 应用程序域到底是什么?

wpf - 如何使用 Dispatcher.Invoke 返回值?

c# - 为什么在不涉及逻辑时使用属性?

reflection - 如何在 Kotlin 中获取变量的运行时类?

c# - 如何在C#中通过字符串访问类成员?