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

对着背影说爱祢 提交于 2019-12-06 18:54:11

问题


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?


回答1:


Try this (found here):

byte[] dllAsArray;

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

    formatter.Serialize(stream, results.CompiledAssembly);

    dllAsArray = stream.ToArray();
}



回答2:


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.




回答3:


You can do this using SOS in WinDbg.

The problem is finding the modules.

If you do get your hands on it, there is a 'SaveModule' command to dump the module/assembly to file.

Good luck :)



来源:https://stackoverflow.com/questions/1872502/how-to-save-a-dynamically-generated-assembly-that-is-stored-in-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!