2012-05-29 60 views
1

我试图动态创建另一个程序集中的类CommandDrawing的实例。 CommandDrawing类的默认构造函数包含对静态方法的调用,这些静态方法位于同一个集合中的另一个类中。创建动态类,但是,当它改掉运行在构造函数中的静态方法调用它倒了,出现异常:动态创建具有构造函数的类,该构造函数调用另一个静态类方法

Exception has been thrown by the target of an invocation. TypeInitializeException`The type initializer for threw an exception.

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

我用下面的代码来创建,我已经成功地使用之前和工作类时静态方法调用是不是有:

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); 
} 
+0

你能提供生成动态类的代码吗? – ivowiblo

+0

是否有内部异常?如果是这样,那是什么?您不应该需要显式加载这两个类。 –

+0

ivowiblo - 抱歉,我正在动态创建一个类的实例,而不是类。 – gisWeeper

回答

1

也许无法加载依赖关系。如果类型初始值设定项使用另一个Assembly中可能发生的类型,因为LoadFile不像您预期​​的那样解决依赖关系。 MSDN says

LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does.

所以我建议使用LoadFrom代替LoadFile

+0

对不起Botz300也没有工作。与相同的错误失败 – gisWeeper