【问题标题】:Load Assembly from bytes in ASP.NET CORE从 ASP.NET CORE 中的字节加载程序集
【发布时间】:2017-03-26 18:28:24
【问题描述】:

我想将 roslyn 生成的程序集放入字节数组 (works),然后将其从这些字节加载到另一个程序集中。它在 net framework 4.6 中工作,但在 .NET CORE 中似乎失败

这里是代码。

 public class Pr2
{
    public static void Main()
    {
        var code = File.ReadAllText(@"E:\VictoriaCMS\WebApplication4\WebApplication4\Controllers\code.cs");
        var tree = SyntaxFactory.ParseSyntaxTree(code);

        var compilation = CreateCompilation(tree);
        var model = compilation.GetSemanticModel(tree);

         ShowLocalDeclarations(tree, model);
         ShowDiagnostics(compilation);
         ExecuteCode(compilation);

    }

    private static void ShowLocalDeclarations(SyntaxTree tree, SemanticModel model)
    {
        var locals = tree.GetRoot()
                         .DescendantNodes()
                         .OfType<LocalDeclarationStatementSyntax>();

        foreach (var node in locals)
        {
            var type = model.GetTypeInfo(node.Declaration.Type);
            Console.WriteLine("{0} {1}", type.Type, node.Declaration);
        }
    }

    private static void ExecuteCode(CSharpCompilation compilation)
    {
        using (var stream = new MemoryStream())
        {
            compilation.Emit(stream);

            var assembly = Assembly.Load(stream.GetBuffer()); 

            var type = assembly.GetType("Greeter");
            var greeter = Activator.CreateInstance(type);
            var method = type.GetMethod("SayHello");
            method.Invoke(greeter, null);
        }
    }

    private static void ShowDiagnostics(CSharpCompilation compilation)
    {
        var diagnostics = compilation.GetDiagnostics();
        foreach (var diagnostic in diagnostics)
        {
            Console.WriteLine(diagnostic.ToString());
        }
    }

    private static CSharpCompilation CreateCompilation(SyntaxTree tree)
    {
        var options = new CSharpCompilationOptions(
                                        OutputKind.DynamicallyLinkedLibrary);

        var reference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);

        var compilation =
            CSharpCompilation.Create("test")
                             .WithOptions(options)
                             .AddSyntaxTrees(tree)
                             .AddReferences(reference);
        return compilation;
    }
}

在 .Net 核心中,由于某种原因,我没有 Assembly.Load(字节 [] 缓冲区)过载。有人可以帮我吗?谢谢。

  var assembly = Assembly.Load(stream.GetBuffer()); 

【问题讨论】:

    标签: c# asp.net .net .net-assembly roslyn


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      相关资源
      最近更新 更多