Generate C # code using Roslyn and .NET Core

Is there a way to generate C # code using Roslyn with .NET Core. I tried using SyntaxFactory from the Microsoft.CodeAnalysis.CSharp package. The problem I'm currently facing is getting the correct formatted code as text.

All the samples I've seen so far use something like

var ws = new CustomWorkspace(); ws.Options.WithChangedOption (CSharpFormattingOptions.IndentBraces, true); var code = Formatter.Format (item, ws); 

The problem is that they all use the Microsoft.CodeAnalysis.CSharp.Workspaces package, which is not compatible with .NET Core at the moment. Are there alternative routes or workarounds for using Roslyn as a code generator with .NET Core?

+6
source share
1 answer

The Microsoft.CodeAnalysis.CSharp.Workspaces 1.3.2 package supports netstandard1.3 , so it must be compatible with .Net Core. But it depends on Microsoft.Composition 1.0.27, which only supports portable-net45+win8+wp8+wpa81 . This framework is compatible with .Net Core, but only if you import it into your project. Json

This means that to do this, the relevant sections of your project.json should look like this:

 "dependencies": { "Microsoft.CodeAnalysis.CSharp.Workspaces": "1.3.2" }, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" } }, "imports": "portable-net45+win8+wp8+wpa81" } } 
+11
source

All Articles