Need to traverse through .cs files in a solution and determine if a function is used
I am looking to write a utility for work where I can take a list of generated function names (we use a custom-code gener开发者_Python百科ation process) and traverse through all of the .cs files within a particular solution and determine if the function is being used. The most difficult thing preventing me from figuring this out is determining whether or not the function is within a commented line or block of code. I planned on just looping through each .cs file and using a streamreader looking for a match on the function name. Is that a good approach to start with?
NOTE: This is a utility that I am looking to write that I can use with various solution files. We have thousands of generated functions from our code-gen utility and I am looking to report on the unused functions.
I would compile the code and use a tool like NDepend on it, no reason to reinvent the wheel.
This process can be very well automated using a CQL (Code Query Language) statement.
I've always gone to:
Edit -> Find and Replace -> Find in Files.
Then a window pops up. You select "Entire Solution" from the 'Look In' drop-down. And then type what you're looking for in the "Find What" field.
EDIT: Oh, I see that you're looking for a list of functions. My apologies. I guess this only works for single items...
The csproj file is xml, so you can use that to get a list of files listed in your projects. You can single out your method names by looking for code outside of braces within a class, then parsing out any modifiers, return types and arguments, then search each file for all the method names found. Bonus for stripping comments out of the file before processing.
I am doing something very similar with C code currently, and its worked out alright.
One thing I've done to find function calls (I have a list of calls where I care if they are called in a file) is as follows:
- Run a regex for the function name on a file that has been stripped of all comments
- If the regex finds a match, check the file line by line (I report what line a function call was found on)
精彩评论