When I try to annotate class, property and method, and then try to retrieve annotated node, only the class one is returned. Why?
Here is the code that annotates
SyntaxAnnotation propertyAnnotation = null;
SyntaxAnnotation classAnnotation = null;
SyntaxAnnotation setMethodAnnotation = null;
document = document
.AnnotateClass(classDeclaration, out classAnnotation)
.AnnotateProperty(propertyDeclaration, out propertyAnnotation)
.AnnotateSetMethod(setMethodDeclaration, out setMethodAnnotation);
I have these extension methods on IDocument defined
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);
}
And if I do
var propertyDeclaration = document.GetAnnotatedNode<PropertyDeclarationSyntax>(propertyAnnotation);
I get an error, but if I try with ClassDeclarationSyntax it works fine.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…