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

Daniel picture Daniel · Dec 9, 2009 · Viewed 9k times · Source

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?

Answer

Roboblob picture Roboblob · May 15, 2013

Try this (found here):

byte[] dllAsArray;

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

    formatter.Serialize(stream, results.CompiledAssembly);

    dllAsArray = stream.ToArray();
}