Roslyn throws The language 'C#' is not supported

泄露秘密 提交于 2019-12-10 01:23:45

问题


I have created a class library project and did some processing and also used Roslyn to generate code.

I use the library in a WPF GUI application as a reference.

These are the NuGet packages:

Build shows no error, however when I use the following code:

private static void GetGenerator()
{
  workspace = new AdhocWorkspace();
  generator = SyntaxGenerator.GetGenerator(workspace, LanguageNames.CSharp);
}

I get an exception:

"The language 'C#' is not supported."

   at: Microsoft.CodeAnalysis.Host.HostWorkspaceServices.GetLanguageServices(String languageName)
   at: Microsoft.CodeAnalysis.Host.Mef.MefWorkspaceServices.GetLanguageServices(String languageName)
   at: Microsoft.CodeAnalysis.Editing.SyntaxGenerator.GetGenerator(Workspace workspace, String language)

According to this and this, I have to copy the CodeAnalysis files locally and add the necessary references. They are there, yet the error occurs.

Is this still a bug that wasn't fixed in the last year?

What else should I do?


回答1:


Most likely it's because you don't reference Microsoft.CodeAnalysis.CSharp.Workspaces in your code, i.e. you never use a type or method in this dll, so MSBuild thinks it's not needed (see e.g. this question).

So what you could do is e.g. add the following line somewhere in your class library project:

var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions);

Then MSBuild should copy Microsoft.CodeAnalysis.CSharp.Workspaces.dll over and everything should be fine. No need to reference the NuGet packages from all the other projects.




回答2:


You have to add the Microsoft.CodeAnalysis package to both the class library project AND the referencing project as well.



来源:https://stackoverflow.com/questions/38204509/roslyn-throws-the-language-c-is-not-supported

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!