我有我的DLL:查找类失败并GetMethod调用抛出NullReferenceException异常
using System;
namespace DLLtest
{
public static class TestDll
{
public static void TestVoid()
{
Console.WriteLine("TestVoid called");
}
}
}
,并在我的节目我做:
Assembly a = Assembly.LoadFile(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DLLtest.dll"));
var b = a.GetType("TestDll").GetMethod("TestVoid");
b.Invoke(null, new object[] { });
我也得到NullReferenceException
的 “var b...
” 行(确实如What is a NullReferenceException, and how do I fix it?所解释的那样,有些东西是空的,但是程序集已经加载并且类应该在那里)。
我尝试添加的BindingFlags,但总是同样的错误......
我不认为应该重复的问题与OP的错误有任何关系。 – RafaelC
@RafaelCardoso空引用副本被滥用。 – LarsTech
我认为OP还需要将'a'作为第一个参数传递给'Invoke'调用,否则'Invoke'将不知道要调用哪个对象。编辑:这个建议是不正确的,问题示例中的方法是静态的。 – cskwrd