'Main' method not found when compiling through Roslyn

前端 未结 1 602
猫巷女王i
猫巷女王i 2021-01-12 14:42

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:

相关标签:
1条回答
  • 2021-01-12 15:42

    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.

    0 讨论(0)
提交回复
热议问题