In VB.NET (or C#) how can I determine programmatically if a public variable in class helper.vb is used anywhere within a project?
Thanks in advance.
In VB.NET (or C#) how can I determine programmatically if a public variable in class helper.vb is used anywhere within a project?
Thanks in advance.
You would need to use reflection and it would be complicated.
Why are you doing this programmaticly? You know that Visual Studio has a "Find all References" feature that can do this for you.
Reflector has the Analyze feature. Or, is this some sort of run time functionality you are after?
Are you talking about doing this before the code is compiled? Doing this against a compiled assembly would probably not be trivial, though tools like Mono.Cecil could help. You would have to actually walk each method and inspect the IL instructions for calls to the get and set methods of the property in question. It might not actually be that bad though, especially if you used Cecil instead of System.Reflection. Cecil is also much faster, as it treats assemblies as files rather than actually loading them into the application domain.
If you're wanting to run this on the actual source code of a project things are a lot different. I don't know much about Visual Studio Add-Ins, but you might be able to invoke the "Find all references" command programmatically and use the results.
There might also be something in System.CodeDom that could help. It looks like you could use a CodeParser
to parse the code into a CodeCompileUnit
, and then from there walk all of the statements in all of the methods and check for related CodePropertyReferenceExpression
s.
From MSDN
The Find object allows you to search for and replace text in places of the environment that support such operations, such as the Code editor.
It is intended primarily for macro recording purposes. The editor's macro recording mechanism uses Find rather than TextSelection.FindPattern so that you can discover the global find functionality, and because it generally is more useful than using the TextSelection Object for such operations as Find-in-files.
If the search operation is asynchronous, such as Find All, then the FindDone Event occurs when the operation completes.
Sub ActionExample()
Dim objFind As Find = objTextDoc.DTE.Find
' Set the find options.
objFind.Action = vsFindAction.vsFindActionFindAll
objFind.Backwards = False
objFind.FilesOfType = "*.vb"
objFind.FindWhat = "<Variable>"
objFind.KeepModifiedDocumentsOpen = False
objFind.MatchCase = True
objFind.MatchInHiddenText = True
objFind.MatchWholeWord = True
objFind.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
objFind.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
objFind.SearchPath = "c:\<Your>\<Project>\<Path>"
objFind.SearchSubfolders = False
objFind.Target = vsFindTarget.vsFindTargetCurrentDocument
' Perform the Find operation.
objFind.Execute()
End Sub
<System.ContextStaticAttribute()> _
Public WithEvents FindEvents As EnvDTE.FindEvents
Public Sub FindEvents_FindDone(ByVal Result As EnvDTE.vsFindResult, _
ByVal Cancelled As Boolean) _
Handles FindEvents.FindDone
Select Case Result
case vsFindResultFound
'Found!
case else
'Not Found
Ens select
End Sub