我完全不了解反射,我试图从数据库记录中调用一个类名,然后加载该类并运行它,但我在哪里出错了,可能是我错过了一些非常愚蠢的东西。
例如,我将我的类放在不同的项目和脚本文件夹中,然后从数据库记录中调用其名称。
className = String.Format("Utilities.Scripts.{0}", script.ScriptClass);
然后在我的主程序中,我有以下代码:
// Get a type from the string
Type type = Type.GetType(className);
// Create an instance of that type
Object obj = Activator.CreateInstance(type);
// Retrieve the method you are looking for
MethodInfo methodInfo = type.GetMethod("start");
// Invoke the method on the instance we created above
methodInfo.Invoke(obj, null);
但是在调试时,我发现我的细节已经传递到GetType(className)中,但是没有进入类型,因此也没有进入obj,这就是错误所在。