Get functions in VS using macros
How to get all the functions you have in a code file in Visual Studio using VS macros? I`m using Visual Studio 2008.
Also I need to get whether function is private protected or public. For now I know I can just parse th开发者_如何学运维e 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.
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.
Getting function accessibility is easy. Following the first article, you have CodeElement object. If it is of type CodeFunction, you can cast it to CodeFunction (or also to CodeFunction2) type. The CodeFunction contains many properties including Access which is what you need. I have modified ShowCodeElement from this article so it only shows functions and also displays their accessibility:
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.
精彩评论