C#反射不调用方法

C#反射不调用方法,c#,reflection,invoke,C#,Reflection,Invoke,我正在使用Unity和reflection,并试图调用某个方法名Start,但我的代码没有调用它 以下是ModLoader.cs: using UnityEngine; using System.Collections; using System.IO; using System.Reflection; using System.Collections.Generic; public class ModLoader : MonoBehaviour { List<MethodInf

我正在使用Unity和reflection,并试图调用某个方法名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[]{  });
            }
        }
    }
}
使用UnityEngine;
使用系统集合;
使用System.IO;
运用系统反思;
使用System.Collections.Generic;
公共类ModLoader:MonoBehavior{
List modMethods=new List();
//用于初始化
无效开始(){
如果(!Directory.Exists(Application.dataPath+“/../Mods”)){
Directory.CreateDirectory(Application.dataPath+“/../Mods”);
}
foreach(目录.GetFiles(Application.dataPath+“/../Mods”、“*.dll”)中的var mod){
var assembly=assembly.LoadFile(mod);
foreach(assembly.GetTypes()中的变量类型){
foreach(类型.GetMethods()中的var方法){
modMethods.Add(方法);
}
}
}
//在所有MOD中执行Start方法
foreach(modMethods中的MethodInfo方法){
打印(方法名称);
if(method.Name==“Start”&&method.GetParameters().Length==0&&method.IsStatic){
调用(null,新对象[]{});
}
}
}
//每帧调用一次更新
无效更新(){
//在所有MOD中执行更新方法
foreach(modMethods中的MethodInfo方法){
if(method.Name==“Update”&&method.GetParameters().Length==0&&method.IsStatic){
调用(null,新对象[]{});
}
}
}
}
这是我的mod(a.dll):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用UnityEngine;
公共班级1{
静态无效开始(){
Log(“helloworld”);
foreach(GameObject进入GameObject.FindObjectsOfType()){
if(go.GetComponent()){
go.GetComponent().material.color=新颜色(1f、0f、0f);
}
}
}
}

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

如果
开始
方法上没有
public
关键字,它将不会是公共的,并且
GetMethods()
默认情况下只查找公共方法


Start
public,或将
type.GetMethods()
更改为
type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
模块中的Start方法是私有的。默认情况下,反射使用公共方法。您需要将其公开:

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);
            }
        }
    }
}

您是否在调试器中仔细检查了它,以查看当
foreach
调用到达您尝试调用的方法时会发生什么?该方法没有传递if语句,我认为这是因为该方法的名称。你为什么不取这个名字?谢谢你的回答!
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);
            }
        }
    }
}
type.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)