Aflred
Aflred

Reputation: 4603

Load Assembly from bytes in ASP.NET CORE

I would like to put a roslyn generated assembly into an array of bytes ( works ), then load it from those bytes into a another assembly.It works in net framework 4.6 but seems to fail in .NET CORE

HEre is the code.

 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;
    }
}

in .Net core i do not have the Assembly.Load (bytes[] buffer) overload for some reason.Can anybody help me? thank you.

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

Upvotes: 4

Views: 1221

Answers (1)

meziantou
meziantou

Reputation: 21377

You can load the assembly from a stream in .NET core using AssemblyLoadContext:

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

Upvotes: 1

Related Questions