我得到了关于我的注释去了哪里的问题的答案(我的注释在哪里?),我已经重读了 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);