【问题标题】:How to load assembly from stream in .NET Core如何从 .NET Core 中的流中加载程序集
【发布时间】:2017-03-30 13:24:57
【问题描述】:

我在 .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

我收到此错误:

无法从“byte[]”转换为“System.Reflection.AssemblyName”

.NET Core 中从 Steam 加载的替代方法是什么? (除了物理保存程序集并加载它)

【问题讨论】:

    标签: c# .net .net-core


    【解决方案1】:

    您可以使用 AssemblyLoadContext 从 .NET 核心中的流中加载程序集:

    // requires "System.Runtime.Loader": "4.0.0",
    protected virtual Assembly LoadAssembly(MemoryStream peStream, MemoryStream pdbStream)
    {
        return System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(peStream, pdbStream);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2023-04-04
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多