简体   繁体   English

如何在roslyn中删除语法树中的标记。 例如从属性中删除虚拟关键字令牌?

[英]How do I remove token in syntax tree in roslyn. e.g. remove virtual keyword token from property?

How do I remove token in syntax tree in roslyn. 如何在roslyn中删除语法树中的标记。 eg remove virtual keyword token from property? 例如从属性中删除虚拟关键字令牌? Roslyn has RemoveNode method but I can't find RemoveToken method or a way to remove token. Roslyn有RemoveNode方法,但我找不到RemoveToken方法或删除令牌的方法。

Found the answer. 找到了答案。 To remove, replace the token with new token of kind none. 要删除,请使用类型为none的新令牌替换令牌。 Awkward but works. 尴尬但有效。

var noneToken = SyntaxFactory.Token(SyntaxKind.None);
node = node.ReplaceToken(token_to_remove, noneToken);

I'm getting an ArgumentException when running the above code. 我在运行上面的代码时遇到了ArgumentException This seems to work (though it may be a bit of a hack): 这似乎有效(虽然它可能有点像黑客):

var node = node.ReplaceToken(dotToken_to_remove, SyntaxFactory.MissingToken(SyntaxKind.DotToken));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM