如何从.NET Core中的流加载程序集 [英] How to load assembly from stream in .NET Core
问题描述
我在.NET Core控制台应用程序上遇到了Roslyn.我试图创建一个程序集并将其编译到内存流中.但是加载它似乎缺少API(通常可以在常规.NET Framework上运行)
I am experiencing with Roslyn on a .NET Core console application. I tried to create an assembly and compile it into a memory stream. But loading it seems to be missing an API (that normally works on the regular .NET Framework)
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { pocoTree }, references: new[] { mscorlib, currentAssembly });
var ms = new MemoryStream();
var emitResult = compilation.Emit(ms);
var ourAssembly = Assembly.Load(ms.ToArray()); // Fails to compile here
我收到此错误:
无法从"byte []"转换为"System.Reflection.AssemblyName"
cannot convert from 'byte[]' to 'System.Reflection.AssemblyName'
.NET Core中从Steam加载的替代方法是什么? (除了实际保存并加载程序集之外)
What is the alternative in .NET Core to load from a steam? (other than physically saving the assembly and loading it)
推荐答案
您可以使用AssemblyLoadContext
从.NET Core中的流中加载程序集:
You can load the assembly from a stream in .NET core using AssemblyLoadContext
:
// requires "System.Runtime.Loader": "4.0.0",
protected virtual Assembly LoadAssembly(MemoryStream peStream, MemoryStream pdbStream)
{
return System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(peStream, pdbStream);
}
这篇关于如何从.NET Core中的流加载程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!