在我的一些测试中,我需要生成一些程序集,然后使用 ICSharpCode.Decompiler “反编译”它们, ICSharpCode.Decompiler 使用Mono.Cecil检查程序集。
出于性能原因,我想在内存中生成程序集以避免磁盘 I/O。
您可以在下面找到一些我打算使用的代码:
var cdp = CodeDomProvider.CreateProvider(CodeDomProvider.GetLanguageFromExtension("cs"));
var p = new CompilerParameters { GenerateInMemory = true } ;
var cr = cdp.CompileAssemblyFromSource(p, sources);
if (cr.Errors.Count > 0)
{
throw new Exception(cr.Errors[0].ErrorText);
}
// !!! I'd like to avoid building / loading the assembly to / from disk
// var assembly = AssemblyDefinition.ReadAssembly(assemblyPath, readerParameters);
// Instead, I'd like to do something like:
Stream assemblyStream = GetAssemblyContentAsStream(cr.CompiledAssembly);
var assembly = AssemblyDefinition.ReadAssembly(assemblyStream, readerParameters);
var dc = new DecompilerContext(assembly.MainModule);
var astBuilder = new AstBuilder(dc);
astBuilder.AddType(typeToBeConverted);
var output = new StringWriter();
astBuilder.GenerateCode(new PlainTextOutput(output));
所以问题是:是否可以实现 GetAssemblyContentAsStream() ?