简体   繁体   English

使用宏在VS中获取函数

[英]Get functions in VS using macros

How to get all the functions you have in a code file in Visual Studio using VS macros? 如何使用VS宏在Visual Studio中获取代码文件中的所有功能? I`m using Visual Studio 2008. 我正在使用Visual Studio 2008。

Also I need to get whether function is private protected or public. 我还需要获取功能是私有保护的还是公共的。 For now I know I can just parse the code and check it on my own, but I want to make it in a proper way and think vs macros environment should allow know all info about functions. 现在,我知道我可以自己解析代码并对其进行检查,但是我想以一种适当的方式进行编码,并认为vs宏环境应允许了解有关函数的所有信息。

See HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro or add-in An maybe HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in would be interesting for you. 请参阅HOWTO:从Visual Studio .NET宏或加载项 导航文件 的代码元素也许如何: 从Visual Studio .NET宏或加载项 导航解决方案的文件对您来说很有趣。

Getting function accessibility is easy. 获取功能可访问性很容易。 Following the first article, you have CodeElement object. 在第一篇文章之后,您将拥有CodeElement对象。 If it is of type CodeFunction, you can cast it to CodeFunction (or also to CodeFunction2) type. 如果类型为CodeFunction,则可以将其强制转换为CodeFunction(或CodeFunction2)类型。 The CodeFunction contains many properties including Access which is what you need. CodeFunction包含许多属性,包括所需的Access。 I have modified ShowCodeElement from this article so it only shows functions and also displays their accessibility: 我已经从本文修改了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

Change it and execute ShowFileCodeModel macro then. 更改它,然后执行ShowFileCodeModel宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM