使用Roslyn和.NET Core生成C#代码

编程入门 行业动态 更新时间:2024-10-15 22:29:36
本文介绍了使用Roslyn和.NET Core生成C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有一种使用Roslyn和.NET Core生成C#代码的方法。我尝试使用Microsoft.CodeAnalysis.CSharp包中的SyntaxFactory。我目前遇到的问题是从文本中获取格式正确的代码。

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

到目前为止,我所看到的所有示例都使用类似

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);

这里的问题是,他们都使用Microsoft.CodeAnalysis.CSharp.Workspaces软件包,目前与.NET Core兼容。将Roslyn用作.NET Core的代码生成器是否有其他替代方法或解决方法?

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

推荐答案

包 Microsoft.CodeAnalysis.CSharp.Workspaces 1.3.2支持 netstandard1.3 ,因此它应与.Net Core兼容。但这取决于 Microsoft.Composition 1.0.27,后者仅支持 portable-net45 + win8 + wp8 + wpa81 。该框架名称与.Net Core兼容,但前提是您将其导入到project.json中。

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

这意味着要使该功能起作用, project.json应该看起来像这样:

That means that to make this work, 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" } }

更多推荐

使用Roslyn和.NET Core生成C#代码

本文发布于:2023-11-13 22:31:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   Roslyn   NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!