We help IT Professionals succeed at work.

How to find the method that contains a specific string in a .net solution?

treehouse2008
on
I have the source code of a .net solution in C# (which includes several projects). I want to find the method and the class that contains a specific string in the program, not in Visual Studio. How can I do this?
Comment
Watch Question

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
In visual studio press  CTRL + SHIFT + f and you will get the search in files.  There you can select one/multiple path for files to search through for the string you want.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Can you expand upon what you mean by, "in the program"?

Author

Commented:
Please note my question "in the program, not in Visual Studio".

Author

Commented:
My question is that: I want to write a program to find the method that contains a specific string.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
So the program when running will search in itself for a string ?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I see. What you are talking about is in fact parsing, which is no trivial task. However, you might be able to take advantage of the C# compiler to do some of the work for you. You can actually invoke the C# compiler within code. I'll have to look around for an example specifically related to parsing, though.

Author

Commented:
I have an existing .net solution (which includes several projects) in C#.
Now I want to to write a new program to find, in the existing .net solution, which method contains a specific string.
I know how to do this in Visual Stuido.
My question is: how can I do this  in my new programming?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Alternatively, since I'm not sure if what I previously suggested can be accomplished within the out-of-the-box API, you could create a quick-and-dirty "parser" whereby you first scan for a line that contains the word "class", then scan for each function name, then scan for your particular string. Each time you find a new function, you overwrite the last one you found--if you haven't found your string yet. If you haven't found your string yet, and you find a new "class" occurrence, then you overwrite the last class you found--and clear out the last function, since it will no longer be applicable. This is *very* quick-and-dirty, though, and probably not the best route to take.

Author

Commented:
kaufmed:  
In the code, how can I konw this is a "function"(method)?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
That's the kind of thing where a parser is really beneficial. But since I suggested "quick-and-dirty..."

The simplest way would probably be to look for a combination of the access modifier (e.g. public, private, protected, etc.) and the name followed by an opening parenthesis. This depends on if you have put access modifiers on all of your methods. Without an access modifier, C# defaults to private. This, however, will also cause you to find any Delegate definitions you might have as well--this shouldn't be too much of a concern since delegates don't have method bodies.

You could go a bit more involved and look for a type (i.e. identifier) followed by a name followed by an opening parenthesis. Or you could go even further and have logic that inspected the stuff inside the parentheses to see if it were a definition of function arguments, although you would have issue with functions that have no arguments.

This is all very hacky.

Author

Commented:
Yeah, the situation of "no arguments" functions  would be an issue.

In tems of "parser", what do you recommend?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I can't really recommend any particular parser as I've never had the need to parse C# code in this manner. All I can offer are some potential candidates. Someone else has done so here: http://stackoverflow.com/questions/81406/parser-for-c-sharp
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Is there some reason that you don't want to use visual studio - the phrase don't reinvent the wheel springs to mind.

Author

Commented:
The reason that I do not want to use VS is that:  I have lots of "string", I want to find the method that concontains the string for each string. If I use VS, I have to search them one by one. But the number of "string" is too big to be handled one by one.

Author

Commented:
Using Parser solved my problem.

Author

Commented:
Thanks. Parser works. I use NRefactory.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.