Is it possible to get the contents of a loaded .net assembly as a byte array or stream?
What I'm trying to do is something similar to (of course the real scenario is much more complex, so just storing the buffer is not an option).
byte[] bytes = GetTheBytes();
Assembly asm = Assembly.Load(bytes);
byte[] bytes2 = GetAssemblyAsByteArray(asm);
Assert.IsTrue(bytes.SequenceEqual(bytes2));
I need to know how to implement the GetAssemblyAsByteArray function.
Edit:
The solution with File.ReadAllBytes() is not good enough because the assembly might be dynamic, and no, I don't have (easy) access to the source (it's automatically generated and I'd prefer not to keep track of it). The comment with serialization might work, but I wouldn't know exactly how to use it. My end goal is to pass the assemblies as /reference options to csc.exe, and the only way I have thought of which works equivalently whether assemblies are dynamic or not is to save all needed assemblies to temporary files.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…