How to remove all Source code comments in my C # solution with roslyn?

I want to remove all comment in my source code in my C # solution with Roslyn. but how do i do this?

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

}
+4
source share
1 answer

Just some extensions for @SLaks answer. You need to expand CSharpSyntaxRewriterand override the method VisitTrivia. And here you need to check the Kindlittle things. Depending on your needs, you should filter single and multi-line comments:

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

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

+6
source

Source: https://habr.com/ru/post/1616907/


All Articles