CSharpCodeProvider: Referencing a different version of referenced assembly

352 views Asked by At

Suppose Project Main has a reference to Project Ref. In Main, I have defined a CSharpCodeProvider and use it to compile code at runtime.

var provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
var parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.dll");
// Rest of the referenced assemblies.

The code which is compiled at runtime, might require a newer version of Project Ref to run correctly. So I tried to add the new Ref.Dll in a relative subfolder (plugins):

    parameters.ReferencedAssemblies.Add(@"d:\project-output-path\plugins\Ref.dll");  

I have also added the following:

AppDomain.CurrentDomain.AppendPrivatePath("plugins");  

Problem is when I try to compile the script dynamically, the Ref.dll in the main folder is being used and causes error.

So, What would be the best way to reference the new Ref project only for my script?
P.S. I really prefer not having to create another AppDomain since the dynamically executing code is coupled with the code loaded in current AppDomain and cannot be separated.

0

There are 0 answers