共用方式為


HOW TO:使用 CodeModel 物件分析 Visual Basic 程式碼

更新:2007 年 11 月

剖析程式碼檔中的文字可能是相當複雜的工作,您可以用 CodeModel2 物件階層架構來取代。您可以使用 CodeModel2 物件進行下列作業:

  • 分析程式碼的結構。

  • 當做記錄程式碼的基礎。

以下的程序假設您知道如何存取巨集開發環境和建立巨集專案。如需詳細資訊,請參閱加入巨集專案對話方塊

命名空間 (Namespace) 是包含在 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
    
    注意事項:

    若要尋找某一個原始程式檔 (Source File) 中的所有命名空間,可以使用與該原始程式檔有關聯之 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
    

尋找類別

類別 (Class) 宣告和命名空間是透過類似的方式擷取。由於類別可以用巢狀方式置於其他類別中,因此您可尋找在命名空間和其他類別中定義的類別。

若要尋找包含在您專案中的所有類別

  • 寫入遞迴方法來找出專案中的類別宣告,如下所示。

    ' 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
    

請參閱

工作

HOW TO:使用 CodeModel 物件建立 C# 類別

概念

Visual Basic 和 C# 應用程式的 CodeModel 物件概觀

使用程式碼模型探索程式碼 (Visual Basic)

使用程式碼模型探索程式碼 (Visual C#)