Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile code at run-time and use assemblies loaded on memory

I need to compile C# code at run-time. I'm 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 the libraries loaded on memory instead of their file addresses. Is it possible?

like image 676
Ali Sepehri.Kh Avatar asked Mar 25 '26 20:03

Ali Sepehri.Kh


1 Answers

If it is an assembly that isn't generated in-memory only, you could use:

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

Or:

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

The Location property has the path to the assembly loaded.

It has to have a hard copy of the assembly, and not just something in memory, so you can't just use generated assemblies for that. You could save the in-memory generated assemblies to disk first if you need to use them.

like image 138
Patrick Hofman Avatar answered Mar 28 '26 09:03

Patrick Hofman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!