当我尝试注释类、属性和方法,然后尝试检索带注释的节点时,只返回类一。为什么?
这是注释的代码
SyntaxAnnotation propertyAnnotation = null;
SyntaxAnnotation classAnnotation = null;
SyntaxAnnotation setMethodAnnotation = null;
document = document
.AnnotateClass(classDeclaration, out classAnnotation)
.AnnotateProperty(propertyDeclaration, out propertyAnnotation)
.AnnotateSetMethod(setMethodDeclaration, out setMethodAnnotation);
我在 IDocument 上定义了这些扩展方法
internal static IDocument AnnotateSetMethod(this IDocument document, MethodDeclarationSyntax method,
out SyntaxAnnotation annotation)
{
annotation = new SyntaxAnnotation();
var newRoot = document.GetSyntaxRoot()
.ReplaceNode(method, method.WithAdditionalAnnotations(annotation));
return document.UpdateSyntaxRoot(newRoot);
}
internal static IDocument AnnotateProperty(this IDocument document, PropertyDeclarationSyntax property,
out SyntaxAnnotation annotation)
{
annotation = new SyntaxAnnotation();
var newRoot = document.GetSyntaxRoot()
.ReplaceNode(property, property.WithAdditionalAnnotations(annotation));
return document.UpdateSyntaxRoot(newRoot);
}
internal static IDocument AnnotateClass(this IDocument document, ClassDeclarationSyntax classDeclaration,
out
SyntaxAnnotation annotation)
{
annotation = new SyntaxAnnotation();
var newRoot = document.GetSyntaxRoot()
.ReplaceNode(classDeclaration, classDeclaration.WithAdditionalAnnotations(annotation));
return document.UpdateSyntaxRoot(newRoot);
}
public static TSyntaxNode GetAnnotatedNode<TSyntaxNode>(this IDocument document, SyntaxAnnotation annotation)
where TSyntaxNode : CommonSyntaxNode
{
return document.GetSyntaxRoot().GetAnnotatedNode<TSyntaxNode>(annotation);
}
如果我这样做
var propertyDeclaration = document.GetAnnotatedNode<PropertyDeclarationSyntax>(propertyAnnotation);
我收到一个错误,但如果我尝试使用 ClassDeclarationSyntax 它工作正常。