【发布时间】:2012-11-29 22:18:28
【问题描述】:
我让main.exe 加载了一个 dll,从加载的 dll 中,我尝试获取主 exe 中定义的静态类,因此 dll 项目中的代码将是:
static class DllClass
{
static DllClass()
{
Assembly assembly = Assembly.GetEntryAssembly(); // line 1
Type type = assembly.GetType("StaticClass"); //line 2
}
}
但是,第 2 行给了我以下异常
System.Reflection.TargetInvocationException:调用的目标已抛出异常。 ---> System.TypeInitializationException:类型初始化程序 'DllClass' 抛出异常。 ---> System.TypeInitializationException:类型初始化程序 'DllClass' 抛出异常。 ---> System.NullReferenceException: 对象引用未设置为对象的实例。
我认为这里的问题是静态类 StatisClass 不知何故在运行时找不到并返回 null。
有什么解决办法吗?
【问题讨论】:
标签: c# exception reflection