I am using Roslyn to compile a solution with run-time generated code. While the solution compiles perfectly when opened from Visual Studio, it fails from Roslyn:
The problem was solved by adding these two lines before attempting to emit the result:
compilation = compilation.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));
compilation = compilation.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
Notice that both AddReferences
and WithOptions
return new Compilation
instances, so it is necessary to re-assign.