title: How to save managed .NET assembly from memory as EXE/DLL file
body: <p>I would like to know if it is at all possible, and if so - how, to save a dynamically loaded .net assembly (it was loaded from a byte array, not a file) to a file on disk (exe/dll depending on assembly) and be able to use it directly (i.e. if it is exe, it will contain all exe headers and be runnable or at least "reflectable").</p>
  
  <p>Also do the loaded assemblies actually get stored in memory exactly as they appear in files, i.e. all the PE EXE headers, resources, etc, i.e. essentially exe/dll file in memory, or do they get stored in memory differently and so it will not be possible to save some region of memory as dll/exe .net assembly? That is, if I can use analogy from images: you may load jpeg file into memory as a bitmap, where the actual bytes representing the image in memory will be quite different from the actual bytes representing the jpeg file. So is it the same with loading assemblies?</p>
  
  <p>And finally how do I get the memory addresses (pointers) to where the assembly is actually located in process's memory?</p>
  
  <p>Just to clarify: I do not have any access to the initial byte array from which assembly was loaded via Assembly.Load().</p>
  
  <p>Here's a <a href="https://stackoverflow.com/questions/2150913/is-it-possible-to-get-the-contents-of-a-loaded-net-assembly-as-a-byte-array-or">related question</a>, but it's not clear from the answer if there may be a way to dump assembly loaded via byte array from memory to a file on disk.</p>
  
  <p>Thank you.</p>