所以,我有一个MethodDeclarationSyntax节点,我将传递给CSharpSyntaxWalker,并带有以下覆盖
public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
LiteralExpressionCollector literalCollector = new LiteralExpressionCollector();
literalCollector.Visit(node.ArgumentList);
if (literalCollector.Literals.Count > 0)
Creations.Add(node, literalCollector.Literals);
}
public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
{
LiteralExpressionCollector literalCollector = new LiteralExpressionCollector();
var assigment = node.ChildNodes().FirstOrDefault(l => l is LiteralExpressionSyntax);
if(assigment != null)
literalCollector.Visit(assigment);
if (literalCollector.Literals.Count > 0)
Assigments.Add(node, literalCollector.Literals);
}
它以下列形式捕获所有ObjectCreationExpressionSyntax:
ResolveBomForMaterialInput rbfmiInput = new ResolveBomForMaterialInput()
但不是以下内容:
FlowStructureScenario flowScenario = null;
flowScenario = new FlowStructureScenario("F", "F:3");
知道为什么会这样吗?我不认为这很重要,但我使用SyntaxWalkerDepth.Token作为深度。
目前使用的版本为:Microsoft.CodeAnalysis 1.3.2
答案 0 :(得分:1)
问题是VisitAssignmentExpression
中的代码在遇到赋值表达式时会立即停止语法遍历。如果要继续遍历其子节点,可以将base.VisitAssignmentExpression(node);
添加到方法中。