How delete all comments of Source code in my c# solution with roslyn?

旧时模样 提交于 2019-12-04 01:43:06

问题


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 ?

}

回答1:


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)

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



来源:https://stackoverflow.com/questions/33850825/how-delete-all-comments-of-source-code-in-my-c-sharp-solution-with-roslyn

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