Is it possible to save a dynamic assembly to disk?

不打扰是莪最后的温柔 提交于 2019-12-01 03:23:18

Look up where BooCompiler is instantiated, change the pipeline from CompileToMemory to CompileToFile

Yes, the AssemblyBuilder class has a Save method for this purpose. You need to use the appropriate mode for this, which is most likely RunAndSave:

AssemblyBuilder builder =
    AppDomain.CurrentDomain.DefineDynamicAssembly(
        name, AssemblyBuilderAccess.RunAndSave);
// build assembly
builder.Save(path);

If you can get the Assembly at runtime.

i.e.

Assembly assembly = typeof(YourDynamicType).Assembly;

You can then cast this assembly into AssemblyBuilder and call the Save method.

AssemblyBuilder assemblyBuilder = (AssemblyBuilder)assembly;
assemblyBuilder.Save(yourPath);

There may be an easier way to do it, but if you don't mind using WinDbg you can save any loaded managed assembly from memory (WinDbg uses the term module, but it works for managed assemblies as well).

Use the !savemodule command with the address of the assembly. If you don't have the address use the lm vm command with the module name. Following that you get a regular DLL assembly, that you can inspect in Reflector.

Of course you may also just look at the IL code in memory.

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