如何:使用 CodeModel 对象分析 Visual Basic 代码

使用 CodeModel2 对象层次结构是分析代码文件中文本的潜在的复杂任务的替代方法。 您可以使用 CodeModel2 对象:

  • 分析代码结构。

  • 作为编写代码的基础。

所遵循的过程假设您知道如何访问宏开发环境并创建宏项目。 有关更多信息,请参见 Add Macro Project Dialog Box

命名空间包含在 CodeModel2 对象的根级别上或嵌套在其他 CodeNamespace 对象中。 这反映了对命名空间的语法约束。 命名空间或者是顶级代码块,或者包含在其他命名空间中。 若要使用下面的过程,必须在 Visual Studio 集成开发环境 (IDE) 中打开一个项目。

备注

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。这些过程是在“常规开发设置”处于活动状态时开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

查找在项目中包含的所有命名空间

  • 编写递归方法以检索应用程序中的所有命名空间。

    CodeNamespace 对象具有一个 Members 属性,该属性包含该命名空间的第一级别的所有 CodeElements

    ' Macro editor
    Sub FindAllNamespaces()
       Dim cm As CodeModel
       cm = DTE.Solution.Projects.Item(1).CodeModel
    
       ' Look for all the namespaces in the CodeElements
       ' of the project.
       Dim list As String
       Dim ce As CodeElement
       For Each ce In cm.CodeElements
          If TypeOf ce Is CodeNamespace Then
             GetNamespace(CType(ce, CodeNamespace), list)
          End If
       Next
    
       MsgBox(list)
    End Sub
    
    Sub GetNamespace(ByVal ns As CodeNamespace, ByRef list As String)
       ' Add this namespace to the list.
       list &= ns.Name & ControlChars.CrLf
       Dim aspace As CodeNamespace
       Dim ce As CodeElement
       ' Look for more namespaces.
       For Each ce In ns.Members
          If TypeOf ce Is CodeNamespace Then
             GetNamespace(CType(ce, CodeNamespace), list)
          End If
       Next
    End Sub
    

    备注

    通过使用与源文件关联的 ProjectItem 对象的 FileCodeModel 属性,可以查找一个源文件中的所有命名空间。使用此方法将要求对 FindAllNamespaces 方法稍作更改。

查找默认命名空间

上述过程只返回已在应用程序中定义的命名空间。 每个 Visual Basic 项目还包含一个默认命名空间。 应用程序中的所有代码元素都包含在默认命名空间中,尽管 CodeElements 集合不包含它的 CodeNamespace 对象。 可以从任意顶级 CodeType 元素检索默认命名空间。

查找您的项目的默认命名空间

  • 测试项目的顶级代码元素。 任何属于 CodeType 的代码元素均为项目返回 CodeNamespace 元素。

    Sub FindDefaultNamespace()
       Dim cm As CodeModel
       cm = DTE.Solution.Projects.Item(1).CodeModel
    
       Dim ce As CodeElement
       Dim ct As CodeType = Nothing
       Dim defNameSpace As CodeNamespace
       For Each ce In cm.CodeElements
          If TypeOf ce Is CodeType Then
             ct = CType(ce, CodeType)
             defNameSpace = ct.Namespace
          End If
       Next
    
       If Not IsNothing(defNameSpace) Then
          MsgBox(defNameSpace.Name)
       End If
    End Sub
    

查找类

类声明和命名空间的检索方式相似。 因为类可以嵌套在其他类中,所以您可以找到既在命名空间又在其他类中定义的类。

查找在您的项目中包含的所有类

  • 编写如下例所示的递归方法,查找项目中的类声明。

    ' Macro editor
    Sub FindAllClasses()
       Dim cm As CodeModel
       cm = DTE.Solution.Projects.Item(1).CodeModel
    
       ' Look for all the namespaces and classes in the 
       ' project.
       Dim list As String
       Dim ce As CodeElement
       For Each ce In cm.CodeElements
          If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then
             ' Determine whether that namespace or class 
             ' contains other classes.
             GetClass(ce, list)
          End If
       Next
    
       MsgBox(list)
    End Sub
    
    Sub GetClass(ByVal ct As CodeElement, ByRef list As String)
       ' ct could be a namespace or a class. Add it to the list
       ' if it is a class.
       If (TypeOf ct Is CodeClass) Then
          list &= ct.Name & ControlChars.CrLf
       End If
    
       ' Determine whether there are nested namespaces or classes that 
       ' might contain other classes.
       Dim aspace As CodeNamespace
       Dim ce As CodeElement
       Dim cn As CodeNamespace
       Dim cc As CodeClass
       Dim elements As CodeElements
       If (TypeOf ct Is CodeNamespace) Then
          cn = CType(ct, CodeNamespace)
          elements = cn.Members
       Else
          cc = CType(ct, CodeClass)
          elements = cc.Members
       End If
    
       For Each ce In elements
          If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then
             GetClass(ce, list)
          End If
       Next
    End Sub
    

请参见

任务

如何:通过使用 CodeModel 对象创建 C# 类

概念

Visual Basic 和 C# 应用程序的 CodeModel 对象概述

使用代码模型查找代码 (Visual Basic)

使用代码模型查找代码 (Visual C#)