1

我正在尝试动态创建CommandDrawing另一个程序集中的类实例。CommandDrawing类默认构造函数包含对同一汇编中另一个类中的静态方法的调用。创建了动态类,但是当它尝试在构造函数中运行静态方法调用时,它会抛出异常:

调用的目标已引发异常。TypeInitializeException`引发异常的类型初始化程序。

我是否必须在两个类中加载,如果是这样,如何加载?

我使用下面的代码来创建我之前成功使用过的类,并且在静态方法调用不存在时工作:

Assembly assemblyCommandDrawing = System.Reflection.Assembly.LoadFile(@"D:\ManifoldInspections.dll");
Type typeCommandDrawing = assemblyCommandDrawing.GetType("InspectionDetails.CommandDrawing");
object cmd = System.Activator.CreateInstance (typeCommandDrawing, new object[] { drawing, DrawingBaseDetail });

CommandDrawing默认构造函数如下所示 - 注意UtilityMapControl.SetupDrawingTableTemplate是我正在调用的静态方法,它落在了这里:

public CommandDrawing(Manifold.Interop.Drawing p_Drawing, InspectionDetails.DrawingBaseDetail p_ClassDetailTemplate)
{
  this.Drawing = p_Drawing;
  //this.ClassDetailTemplate = p_ClassDetailTemplate.GetType();
  this.ClassDetailTemplate = p_ClassDetailTemplate;
  ManifoldInspections.Utility.UtilityMapControl.SetupDrawingTableTemplate(this.Drawing, p_ClassDetailTemplate);
}
4

1 回答 1

1

可能无法加载依赖项。如果类型初始值设定项使用来自另一个程序集的类型,可能会发生这种情况,因为LoadFile它不会像您预期的那样解决依赖关系。MSDN 说

LoadFile不会将文件加载到LoadFrom上下文中,并且不会像该LoadFrom方法那样使用加载路径解析依赖关系。

所以我建议使用LoadFrom而不是LoadFile.

于 2012-05-29T14:32:14.270 回答