Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compile and execute C# expression without saving the assembly to disk?

I can compile, get an instance and invoke a method of any C# type programmaticaly. There lots of info on that, including the StackOverflow (How can I evaluate a C# expression dynamically?). My problem is that I'm in the web environment and cannot save anything to /bin directory. I can compile "in-memory" as the above mentioned link suggests but then I won't be able to "unload" my custom assembly from the current AppDomain. After a while that will become a huge memory problem. Is it possible to open a new AppDomain, compile new assembly "in-memory", evaluate some expression or access some member of that assembly inside of that new AppDomain and kill that AppDomain safely when done, all that without saving anything to a hard drive? Thanks in advance for any links, suggestions, etc.

like image 850
Sasha Avatar asked May 10 '10 18:05

Sasha


2 Answers

Have you taken a look at the DynamicMethod type in .Net? This type creates garbage collection enabled in memory methods using Reflection.Emit. This sounds like exactly what you're looking for.

like image 87
JaredPar Avatar answered Oct 07 '22 16:10

JaredPar


Rick Strahl has a pretty good post on the subject here.

like image 35
driis Avatar answered Oct 07 '22 14:10

driis