Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a dynamically generated assembly that is stored in-memory?

I want to get my hands on an assembly by saving it to disc or reflect it at runtime. The assembly is generated dynamically in memory by third party.

Does anyone know how to do this?

like image 502
Daniel Avatar asked Dec 09 '09 08:12

Daniel


2 Answers

Try this (found here):

byte[] dllAsArray;

using (MemoryStream stream = new MemoryStream())
{
    BinaryFormatter formatter = new BinaryFormatter();

    formatter.Serialize(stream, results.CompiledAssembly);

    dllAsArray = stream.ToArray();
}
like image 94
Roboblob Avatar answered Oct 16 '22 01:10

Roboblob


It's been a while since I did this, I'm guessing that the program that hands you the DLL uses CompilerParameters.GenerateInMemory=True.

Thing is however that the dll does actually get saved on disk in some temp folder for a brief while (while it runs or something... ) because that is how the C# compiler works (worked?).

I can remember this being an issue for me back then but I'm having trouble getting back the details now, gimme a sec. You can probablby find this out with ProcessExplorer or similar tools to see which files were saved/ touched.

like image 26
gjvdkamp Avatar answered Oct 16 '22 02:10

gjvdkamp