我有包含 MyMethod() 的 Tools.dll 文件,如下所示:
public void MyMethod()
{
global::System.Windows.Forms.MessageBox.Show("Sth");
}
Run Code Online (Sandbox Code Playgroud)
现在,我尝试从另一个文件运行此汇编方法:
System.Reflection.Assembly myDllAssembly = System.Reflection.Assembly.LoadFile(@"PATH\Tools.dll");
myDllAssembly.GetType().GetMethod("MyMethod").Invoke(myDllAssembly, null); //here we invoke MyMethod.
Run Code Online (Sandbox Code Playgroud)
运行后发生“System.NullReferenceException”。它说“对象引用未设置到对象的实例。”
那么我该如何解决它?
我确信这个 .dll 构建事实没有问题。
注意:汇编代码来自:http://www.codeproject.com/Articles/32828/Using-Reflection-to-load-unreferenced-assemblies-a