Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create a compilation in Roslyn from source code

For test purposes, I need to get a System.Reflection.Assembly from a string source which contains a source code. I am using Roslyn:

SyntaxTree tree = CSharpSyntaxTree.ParseText(source);
CSharpCompilation compilation = CSharpCompilation.Create("TestCompilation", new[] { tree });

Assembly assembly = null;
using (var stream = new MemoryStream())
{
    var emitResult = compilation.Emit(stream);
    if (!emitResult.Success)
    {
        var message = emitResult.Diagnostics.Select(d => d.ToString())
            .Aggregate((d1, d2) => $"{d1}{Environment.NewLine}{d2}");

        throw new InvalidOperationException($"Errors!{Environment.NewLine}{message}");
    }

    stream.Seek(0, SeekOrigin.Begin);
    assembly = Assembly.Load(stream.ToArray());
}

As you can see my attempt here is to emit a CSHarpCompilation object so that I can get the Assembly later. I am trying to do this with:

var source = @"
  namespace Root.MyNamespace1 {
    public class MyClass {
    }
  }
";

Emit errors

But I fail at var emitResult = compilation.Emit(stream) and enter the conditional which shows the error. I get 1 warning and 3 errors:

  • Warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
  • (3,34): Error CS0518: Predefined type 'System.Object' is not defined or imported
  • (3,34): Error CS1729: 'object' does not contain a constructor that takes 0 arguments
  • Error CS5001: Program does not contain a static 'Main' method suitable for an entry point

So it seems I need to add reference to mscorelib and it also seems like I need to tell Roslyn that I want to emit a class library, not an executable assembly. How to do that?

like image 913
Andry Avatar asked Mar 31 '17 12:03

Andry


1 Answers

You're missing a metadata reference to mscorlib and you can change the compilation options via CSharpCompilationOptions.

Create your compilation as follows:

var Mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = CSharpCompilation.Create("TestCompilation",
    syntaxTrees: new[] { tree }, references: new[] { Mscorlib }, options: options);
like image 58
JoshVarty Avatar answered Oct 27 '22 03:10

JoshVarty