【问题标题】:SyntaxWalker Visitor doesn't visit MethodsSyntaxWalker 访问者不访问方法
【发布时间】:2016-08-04 09:17:11
【问题描述】:

我有这个助行器

internal class MyWalker : CSharpSyntaxWalker
{
            public int MethodCount { get; private set; }

    public MyWalker() : base(Microsoft.CodeAnalysis.SyntaxWalkerDepth.Trivia)
    { }

    public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
    {
         MyMethodCount ++;
    }

    public override void VisitClassDeclaration(ClassDeclarationSyntax node)
    {
        Class++;
        Complexity++;
    }
}

我用这个代码呼叫步行者

    var code = @"
    public class foo
    {
        public void MyMethod()
        {

        }
    }
";

  SyntaxTree node = CSharpSyntaxTree.ParseText(source);
  var root = node.GetRoot();

  var walker = new MyWalker();
  walker.Visit(root);

但从未调用访问 MethodDeclaration。

为了确保我与 SyntaxTree Analyzer 进行比较,我可以看到 roslyn 应该将其识别为方法声明。

我错过了什么?

【问题讨论】:

  • GetRoot 中有什么内容?
  • 它只是现有的我更改代码的包装器。

标签: c# roslyn roslyn-code-analysis microsoft.codeanalysis


【解决方案1】:

我找到了。我在访问类方法中没有基本调用。所以只是我的一个错误

应该是这样的

   public override void VisitClassDeclaration(ClassDeclarationSyntax node)
        {
[...]
            base.VisitClassDeclaration(node); // this was missing
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 2012-12-20
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    相关资源
    最近更新 更多