【问题标题】:Using Roslyn how to enumerate members in a Visual Basic document of Visual Studio 2017使用 Roslyn 如何在 Visual Studio 2017 的 Visual Basic 文档中枚举成员
【发布时间】:2018-04-30 06:29:03
【问题描述】:

为了枚举 C# 文档中的成员,您可以使用 Roslyn,如下所示:

var members = SyntaxTree.GetRoot().DescendantNodes().OfType<MemberDeclarationSyntax>();

Visual Basic 文档的等价物是什么?

【问题讨论】:

    标签: vb.net roslyn


    【解决方案1】:

    据我所知,Microsoft.CodeAnalysis.VisualBasic.Syntax 中与MemberDeclarationSyntax 最接近的等价物是DeclarationStatementSyntax。这意味着以下代码应该可以工作:

    var members = SyntaxTree.GetRoot().DescendantNodes().OfType<DeclarationStatementSyntax>();
    

    虽然这会给您提供的不仅仅是成员声明。如果您只想要成员,则必须手动过滤掉它们。其代码可能类似于:

    var members = SyntaxTree.GetRoot().DescendantNodes().Where(node =>
        node is EnumMemberDeclarationSyntax ||
        node is FieldDeclarationSyntax ||
        node is IncompleteMemberSyntax ||
        node is MethodBaseSyntax || 
        …);
    

    【讨论】:

    • 非常感谢。这样可行。现在我正在努力确定成员名称、StartLine、EndLine。
    • @JoginderSNahil 如果您认为我的回答确实回答了您的问题,您应该accept it
    • 如何使用会员来确定会员的姓名、起点、终点?
    • @JoginderSNahil 你应该问一个新问题。
    • 我提出了新问题。 stackoverflow.com/questions/50120799/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多