2010-01-07 77 views
2

如何使用VS宏獲取Visual Studio代碼文件中的所有函數? 我真的使用Visual Studio 2008使用宏在VS中獲取函數

而且我需要的功能是保護私有或公共。現在我知道我可以解析代碼並單獨檢查它,但我想以適當的方式進行驗證,並且認爲vs宏環境應該允許瞭解關於函數的所有信息。

回答

1

請參閱HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro or add-in 也許HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in將很有趣。

取得功能輔助功能是很容易。在第一篇文章之後,您有CodeElement對象。如果它是CodeFunction類型,則可以將其轉換爲CodeFunction(或也可以轉換爲CodeFunction2)類型。 CodeFunction包含許多屬性,包括您需要的Access。我從這篇文章修改ShowCodeElement所以它只能顯示功能,同時還顯示它們的可訪問:

Private Sub ShowCodeElement(ByVal objCodeElement As CodeElement) 

    Dim objCodeNamespace As EnvDTE.CodeNamespace 
    Dim objCodeType As EnvDTE.CodeType 
    Dim objCodeFunction As EnvDTE.CodeFunction 

    If TypeOf objCodeElement Is EnvDTE.CodeNamespace Then 

     objCodeNamespace = CType(objCodeElement, EnvDTE.CodeNamespace) 
     ShowCodeElements(objCodeNamespace.Members) 

    ElseIf TypeOf objCodeElement Is EnvDTE.CodeType Then 

     objCodeType = CType(objCodeElement, EnvDTE.CodeType) 
     ShowCodeElements(objCodeType.Members) 

    ElseIf TypeOf objCodeElement Is EnvDTE.CodeFunction Then 

     Try 
      Dim msg As String = objCodeElement.FullName & vbCrLf 
      Dim cd As EnvDTE.CodeFunction = DirectCast(objCodeElement, CodeFunction) 
      Select Case cd.Access 
       Case vsCMAccess.vsCMAccessDefault 
        msg &= "Not explicitly specified. It is Public in VB and private in C#." 
       Case Else 
        msg &= cd.Access.ToString 
      End Select 
      MsgBox(msg) 
     Catch ex As System.Exception 
      ' Ignore 
     End Try 
    End If 

End Sub 

改變它,執行ShowFileCodeModel然後宏。

+0

感謝分享,我已經更新了問題請看看。 – 2010-01-07 14:25:29

+0

我已經更新了答案 – 2010-01-08 09:35:25