Loading an assembly generated by the Roslyn compiler

8

I'm generating a Greeter.dll using the Roslyn compiler. My problem occurs trying to load the DLL file.

Here's the code:

using System;

using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

using System.IO;
using System.Reflection;
using System.Linq;

namespace LoadingAClass
{
    class Program
    {
        static void Main(string[] args)
        {
            var syntaxTree = SyntaxTree.ParseCompilationUnit(@"
class Greeter
{
    static void Greet()
    {
        Console.WriteLine(""Hello, World"");
    }
}");

            var compilation = Compilation.Create("Greeter.dll",
                syntaxTrees: new[] { syntaxTree },
                references: new[] {
                    new AssemblyFileReference(typeof(object).Assembly.Location),
                    new AssemblyFileReference(typeof(Enumerable).Assembly.Location),
                });

            Assembly assembly;
            using (var file = new FileStream("Greeter.dll", FileMode.Create))
            {
                EmitResult result = compilation.Emit(file);
            }

            assembly = Assembly.LoadFile(Path.Combine(Directory.GetCurrentDirectory(), @"Greeter.dll"));
            Type type = assembly.GetType("Greeter");
            var obj = Activator.CreateInstance(type);

            type.InvokeMember("Greet",
                BindingFlags.Default | BindingFlags.InvokeMethod,
                null,
                obj,
                null);

            Console.WriteLine("<ENTER> to continue");
            Console.ReadLine();

        }
    }
    // Thanks, http://blogs.msdn.com/b/csharpfaq/archive/2011/11/23/using-the-roslyn-symbol-api.aspx
}

The error message occurs on the line assembly = Assembly.LoadFile(Path.Combine(Directory.GetCurrentDirectory(), @"Greeter.dll")); and reads

Im Modul wurde ein Assemblymanifest erwartet. (Ausnahme von HRESULT: 0x80131018)

Which roughly translates to

An assembly manifest was expected in the module.

Does anyone know what I'm missing here?

c#
assemblies
roslyn
asked on Stack Overflow May 25, 2012 by lowerkey

5 Answers

15

I stumbled across this and, even though you have an accepted answer, I don't think it's helpful in general. So, I'll just leave this here for future searchers like myself.

The problem with the code is two things, which you would have found out by looking at the returned value from

EmitResult result = compilation.Emit(file);

If you look at the properties on the EmitResult object, you would have found that there were 2 errors in the results.Diagnostics member.

  1. Main method not found
  2. Couldn't find class Console

So, to fix the problem, 1. You need to mark the output as a dll 2. You need to add 'using System;' to the code you're passing into the API or say 'System.Console.WriteLine'

The following code works making changes to fix those two issues:

        var outputFile = "Greeter.dll";
        var syntaxTree = SyntaxTree.ParseCompilationUnit(@"
 // ADDED THE FOLLOWING LINE
using System;

class Greeter
{
    public void Greet()
    {
        Console.WriteLine(""Hello, World"");
    }
}");
        var compilation = Compilation.Create(outputFile,
            syntaxTrees: new[] { syntaxTree },
            references: new[] {
                new AssemblyFileReference(typeof(object).Assembly.Location),
                new AssemblyFileReference(typeof(Enumerable).Assembly.Location),
            },

// ADDED THE FOLLOWING LINE
            options: new CompilationOptions(OutputKind.DynamicallyLinkedLibrary));

        using (var file = new FileStream(outputFile, FileMode.Create))
        {
            EmitResult result = compilation.Emit(file);
        }

        Assembly assembly = Assembly.LoadFrom("Greeter.dll");

        Type type = assembly.GetType("Greeter");
        var obj = Activator.CreateInstance(type);

        type.InvokeMember("Greet",
            BindingFlags.Default | BindingFlags.InvokeMethod,
            null,
            obj,
            null);

        Console.WriteLine("<ENTER> to continue");
        Console.ReadLine();
answered on Stack Overflow Jul 13, 2012 by Sef
2

I have been adding Roslyn support to the O2 Plarform and here is how you can use its Roslyn support to compile (code), create (and assembly) and invoke (its method) one line of code:

return @"using System; class Greeter { static string Greet() {  return ""Another hello!!""; }}"
        .tree().compiler("Great").create_Assembly().type("Greeter").invokeStatic("Greet"); 

//O2Ref:O2_FluentSharp_Roslyn.dll

Here is a version that executes a code snippet that looks like yours (I added a return value):

panel.clear().add_ConsoleOut();
var code = @"
using System;
class Greeter
{
    static string Greet()
    { 
        Console.WriteLine(""Hello, World""); 
        return ""hello from here"";
    }
}";
var tree = code.astTree();
if (tree.hasErrors())
    return tree.errors();   

var compiler = tree.compiler("Great")
                   .add_Reference("mscorlib");

if (compiler.hasErrors()) 
    return compiler.errors();    

var assembly  =tree.compiler("Great")
                   .create_Assembly();

return assembly.type("Greeter")
               .invokeStatic("Greet"); 

//O2Ref:O2_FluentSharp_Roslyn.dll
//O2File:_Extra_methods_Roslyn_API.cs
//O2File:API_ConsoleOut.cs

For a couple more details and screenshots of what this looks like, see this blog post: 1 line to compile, create and execute: O2 Script to use Roslyn to Dynamically compile and execute a method

UPDATE: see http://blog.diniscruz.com/search/label/Roslyn for a large number number of Roslyn related posts and tools (created using the O2 Platform)

answered on Stack Overflow Jun 11, 2012 by Dinis Cruz • edited Feb 15, 2013 by Dinis Cruz
1

There is a new API for the References that looks like this:

var compilation = Compilation.Create(outputFile,
    syntaxTrees: new[] { syntaxTree },
    references: new[] {
        new MetadataFileReference(typeof(object).Assembly.Location),
        new MetadataFileReference(typeof(Enumerable).Assembly.Location),
    },
    options: new CompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);

This is with the latest Roslyn-CTP 2012 in Sept...

answered on Stack Overflow Jul 7, 2013 by etgryphon
0

This code worked beautifully:

using System;

using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

using System.IO;
using System.Reflection;
using System.Linq;

namespace LoadingAClass
{
    class Program
    {
        static void Main(string[] args)
        {
            var syntaxTree = SyntaxTree.ParseCompilationUnit(@"
using System;
namespace HelloWorld
{
    class Greeter
    {
        public static void Greet()
        {
            Console.WriteLine(""Hello, World"");
        }
    }
}");

            string dllPath = Path.Combine(Directory.GetCurrentDirectory(), "Greeter.dll");
            string pdbPath = Path.Combine(Directory.GetCurrentDirectory(), "Greeter.pdb");

            var compilation = Compilation.Create(dllPath,
                new CompilationOptions(
                    assemblyKind: AssemblyKind.DynamicallyLinkedLibrary
                ))
                .AddSyntaxTrees( syntaxTree )
                .AddReferences(new AssemblyFileReference(typeof(object).Assembly.Location))
                .AddReferences(new AssemblyFileReference(typeof(Enumerable).Assembly.Location));

            EmitResult result;

            using (FileStream dllStream = new FileStream(dllPath, FileMode.OpenOrCreate))
            using (FileStream pdbStream = new FileStream(pdbPath, FileMode.OpenOrCreate))
            {
                result = compilation.Emit(
                    executableStream: dllStream,
                    pdbFileName: pdbPath,
                    pdbStream: pdbStream);
            }

            if (result.Success)
            {
                //assembly = Assembly.LoadFile(Path.Combine(Directory.GetCurrentDirectory(), @"Greeter.dll"));
                Assembly assembly = Assembly.LoadFrom(@"Greeter.dll");

                Type type = assembly.GetType("HelloWorld.Greeter");
                var obj = Activator.CreateInstance(type);

                type.InvokeMember("Greet",
                    BindingFlags.Default | BindingFlags.InvokeMethod,
                    null,
                    obj,
                    null);
            }
            else
            {
                Console.WriteLine("No Go");
                Console.WriteLine(result.Diagnostics.ToString());
            }

            Console.WriteLine("<ENTER> to continue");
            Console.ReadLine();

        }
    }
    // Thanks, http://blogs.msdn.com/b/csharpfaq/archive/2011/11/23/using-the-roslyn-symbol-api.aspx
    // Thanks, http://social.msdn.microsoft.com/Forums/en-US/roslyn/thread/d620a4a1-3a90-401b-b946-bfa1fc6ad7a2
}
answered on Stack Overflow May 25, 2012 by lowerkey • edited May 25, 2012 by lowerkey
-1

Turns out I needed to create a pdb file.

using (FileStream dllStream = new FileStream(dllPath, FileMode.OpenOrCreate))
using (FileStream pdbStream = new FileStream(pdbPath, FileMode.OpenOrCreate))
{
    result = compilation.Emit(
       executableStream: dllStream,
       pdbFileName: pdbPath,
       pdbStream: pdbStream);
}
answered on Stack Overflow May 25, 2012 by lowerkey

User contributions licensed under CC BY-SA 3.0