问题
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