我正在 .NET Core 控制台应用程序上体验 Roslyn。我试图创建一个程序集并将其编译成内存流。但是加载它似乎缺少一个 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
我收到这个错误:
cannot convert from 'byte[]' to 'System.Reflection.AssemblyName'
.NET Core 中从 Steam 加载的替代方法是什么? (除了物理保存程序集并加载它)
最佳答案
您可以使用 AssemblyLoadContext
从 .NET core 中的流加载程序集:
// requires "System.Runtime.Loader": "4.0.0",
protected virtual Assembly LoadAssembly(MemoryStream peStream, MemoryStream pdbStream)
{
return System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(peStream, pdbStream);
}
关于c# - 如何从 .NET Core 中的流加载程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40384619/