【问题标题】:Get functions in VS using macros在 VS 中使用宏获取函数
【发布时间】:2010-01-07 07:46:00
【问题描述】:

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

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

【问题讨论】:

    标签: visual-studio macros


    【解决方案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 宏。

    【讨论】:

    • 感谢分享,我已经更新了问题,请看一下。
    猜你喜欢
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    相关资源
    最近更新 更多