C# 错误?使用通用类型加载组件
我有一个像这样动态加载程序集的类:
Assembly asm = Assembly.LoadFile(args[1]);
runner.RunTestOnAssembly(asm);
然后另一个类在该程序集上运行测试:
foreach (var cspecType in asm.GetTypes())
{
RunTestOnType(cspecType);
}
加载的程序集引用同一文件夹中的其他程序集,[Debug\Tests]以及加载的程序该程序集位于[调试]文件夹中。
加载的程序集 (CalcSpecAsm):
public class CalcSpec : CSpecFacade<ICalc>
{
public CalcSpec()
: base(new Calc())
{
}
}
引用的程序集 (CalcAsm):
public class Calc : ICalc
{
/// <summary>
/// Initalisation constructor
/// resets the Total value.
/// </summary>
public Calc()
{
Total = 0;
}
.....
CSpecFacade 在另一个程序集中引用。
现在的问题是,如果我加载程序集 CalcSpecAsm,我会在 GetTypes() 上收到程序集加载器错误,它无法解析对 CalcAsm 程序集的引用。
问题是这行代码: public class CalcSpec : CSpecFacade
如果我删除通用参数 ICalc 并在构造函数中初始化它,那么它可以完美工作,但这不是我想要的。
但是:如果我将 loaderAssembly 程序复制到 [Debug\Test] 然后运行它,一切正常。
如何解决这个问题,是什么原因造成的?
I have an class that loads the assembly dynamically like so:
Assembly asm = Assembly.LoadFile(args[1]);
runner.RunTestOnAssembly(asm);
then another class runs the test on that assembly:
foreach (var cspecType in asm.GetTypes())
{
RunTestOnType(cspecType);
}
The loaded assembly references other assemblies in the same folder, [Debug\Tests] and the program that loads the assembly is in the [Debug] folder.
The loaded Assembly (CalcSpecAsm):
public class CalcSpec : CSpecFacade<ICalc>
{
public CalcSpec()
: base(new Calc())
{
}
}
The referenced Assembly (CalcAsm):
public class Calc : ICalc
{
/// <summary>
/// Initalisation constructor
/// resets the Total value.
/// </summary>
public Calc()
{
Total = 0;
}
.....
The CSpecFacade is referenced in another assembly.
And now the problem is that if I load the assembly CalcSpecAsm I get assembly loaderError on GetTypes() that it cannot resolve a reference to CalcAsm assembly.
The problem is this line of code: public class CalcSpec : CSpecFacade<ICalc>
If I remove the generic arg ICalc and initize it in the constructor then it works perfect but that's not what I'm after.
BUT: If i copy the loaderAssembly program to the [Debug\Test] and then run it, everything works fine.
How to resolve the problem, and what causes it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您从其他位置加载程序集时,可能会收到错误消息,指出无法加载程序集或其依赖项。
这意味着在加载程序集时,.Net 运行时无法解析程序集的所有依赖项。
所以你需要提供一种方法来解决这个问题。
首先,您必须像这样将解析事件添加到您的应用程序域
,然后在这种情况下您将必须加载您的依赖项。
希望这会有所帮助。
when you load assemblies from other locations, you might get errors saying that assembly could not be loaded or it's dependencies could not be loaded.
This means that when loading your assembly .Net Runtime was not able to resolve all dependencies of your assembly.
So you need to provide a way to resolve this.
First you will have to add resolve event to your appdomain like this
Then in that event you will have to load your dependencies.
Hope this will help.