Compile code at runtime and use assemblies loaded into memory

I need to compile C # code at runtime. I am using the code like this:

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("MyLibrary.dll");    // File Path on Hard Drive
...

      

But I want to use libraries loaded into memory, not their file addresses. Is it possible?

+3


source to share


1 answer


If it is an assembly that is not only generated in memory, you can use:

parameters.ReferencedAssemblies.Add
( typeof(ClassInAssemblyYouWantToAdd).Assembly.Location
);

      

Or:



parameters.ReferencedAssemblies.Add
( Assembly.Load("Full.Qualified.Assembly.Name").Location
);

      

The property Location

has the path to the loaded assembly.

It must have a hard copy of the assembly, not just something in memory, so you can't just use generated assemblies to do this. First you can save the assembled assemblies to disk if you need to use them.

+3


source







All Articles