Class compilation at runtime fails when CompilerParameters.GenerateInMemory == true

I compile dynamic assembly at runtime. It must reference another dll. Everything is working fine while I set the OutputAssembly in my CompilerParameters. But as soon as I set GenerateInMemory = true; he fails:

var compilerParameters = new CompilerParameters();
if( compileInMemory )
    compilerParameters.GenerateInMemory = true;
else
    compilerParameters.OutputAssembly = "<my_dynamic_dll_path>";
compilerParameters.ReferencedAssemblies.Add( "<other_dll_path>" );
var compilerResults = new CSharpCodeProvider().CompileAssemblyFromDom( compilerParameters, codeCompileUnit );

// Here: compilerResults.Errors.HasErrors == false

foreach( var type in compilerResults.CompiledAssembly.GetTypes() )
{
     // Exception:
     // Unable to load one or more of the requested types.
     // Retrieve the LoaderExceptions property for more information.
}

LoaderExceptions says that "other_dll" was not found. Why does it work until I compile in memory and what should I do to make it work in memory?

+5
source share
1 answer

GenerateInMemory , Assembly.Load(Byte []). AppDomain.AssemblyResolve, "other_dll".

+5

All Articles