如何使用 roslyn 删除我的 c# 解决方案中源代码的所有注释? [英] How delete all comments of Source code in my c# solution with roslyn?

查看:51
本文介绍了如何使用 roslyn 删除我的 c# 解决方案中源代码的所有注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Roslyn 在我的 C# 解决方案中删除源代码中的所有注释.但我该怎么做?

I want to delete all comment in my source code in my C# solution with Roslyn. but How should i do that ?

public void DeleteComment()
{
        var code = File.ReadAllText("code.cs");
        SyntaxTree tree = SyntaxFactory.ParseSyntaxTree(code);
        ///Delete Comments ?

}

推荐答案

只是@SLaks 答案的一些扩展.您需要扩展 CSharpSyntaxRewriter 并覆盖 VisitTrivia 方法.在这里,您需要检查琐事的Kind.根据您的需要,您应该过滤单行和多行注释:

Just some extensions to @SLaks' answer. You need to extend CSharpSyntaxRewriter and override the VisitTrivia method. And here you'll need to check the Kind of the trivia. Depending on your needs, you should filter for single and multiline comments:

trivia.IsKind(SyntaxKind.SingleLineCommentTrivia) || trivia.IsKind(SyntaxKind.MultiLineCommentTrivia)

并返回 default(SyntaxTrivia) 以将它们从树中删除.

And return default(SyntaxTrivia) to remove them from the tree.

这篇关于如何使用 roslyn 删除我的 c# 解决方案中源代码的所有注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆