0

我得到了关于我的注释去了哪里的问题的答案(我的注释在哪里?),我已经重读了 20 次,但我仍然不清楚为什么这段代码不起作用,为什么我不能得到注释财产,问题出在哪里。我已经内联了代码,这样更容易阅读......

SyntaxAnnotation propertyAnnotation = null;
SyntaxAnnotation classAnnotation = null;

//first annotate class
classAnnotation = new SyntaxAnnotation();

var newRoot = document.GetSyntaxRoot()
                .ReplaceNode(classDeclaration, classDeclaration.WithAdditionalAnnotations(classAnnotation));

document = document.UpdateSyntaxRoot(newRoot);

//now annotate property 
//first look for classNode that was annotated in previous step
var classNode = document.GetAnnotatedNode<ClassDeclarationSyntax>(classAnnotation);

//now annotate property
propertyAnnotation = new SyntaxAnnotation();
var newClassNode = classNode.ReplaceNode(propertyDeclaration, propertyDeclaration.WithAdditionalAnnotations(propertyAnnotation));

newRoot = document.GetSyntaxRoot()
                     .ReplaceNode(classNode, newClassNode);

document = document.UpdateSyntaxRoot(newRoot);

//Works 
newClassNode = document.GetAnnotatedNode<ClassDeclarationSyntax>(classAnnotation);

//Fails here????
var newPropertyNode = document.GetAnnotatedNode<PropertyDeclarationSyntax>(propertyAnnotation);
4

1 回答 1

0

问题在于:

var newClassNode = classNode.ReplaceNode(propertyDeclaration, propertyDeclaration.WithAdditionalAnnotations(propertyAnnotation));

因为您已经注释了类声明,所以您永远不会propertyDeclaration在该调用中找到。它是具有原始树的一部分classDeclaration,而不是带注释的树。将propertyDeclaration注释添加到classDeclaration.

于 2013-11-06T21:46:22.100 回答