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?
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.