使用宏在VS中获取函数

时间:2010-01-07 07:46:00

标签: visual-studio macros

如何使用VS宏获取Visual Studio中代码文件中的所有功能? 我正在使用Visual Studio 2008.

此外,我需要了解功能是私有保护还是公共功能。现在我知道我可以解析代码并自己检查它,但是我想以正确的方式进行检查并认为vs宏环境应该允许知道有关函数的所有信息。

1 个答案:

答案 0 :(得分: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宏。