Get method contents from cs file

I have a requirement to get the contents of each method in the cs file to a string. I am looking for, when you have the input of a cs file, the dictionary is returned with the method name as the key and the body of the method as the value.

I tried Regex and thinking without success, can anyone help?

thanks

+4
source share
6 answers

I don't know if this will be useful for you, but Visual Studio Addins includes an EnvDTE object that gives you full access to VB and C # parsers. See Code Discovery Using a Code Model

I touched it tangentially several years ago, I don’t know how difficult it is to use it or how effective it is, but it looks like it will give you what you need.

The code model allows customers to automate the implementation of the parser for Visual Studio languages to detect high-level definitions in the project, such as classes, interfaces, structures, methods, properties, and so on.

If you read the full article, it tells how to extract the full text from a file for a function

Hope this helps :)

+5
source

Assuming the file is valid (i.e. compiles), you can start by reading the entire file into a string.

From your question, I understand that you are interested in method names, not class names. Then you need a regular expression that will give you all instances of public | protected | private, optional virtual / override keywords, etc., MethodName, (, optional parameters). It would help if you were coding conventions, so you can assume that all the determination methods have always been on the same line rather than scattered across multiple lines.

Once you do this, it's just a matter of counting {and} to get the body of the function.

And one last tip: Beware of assumptions . They have an unpleasant habit of biting you in the butt.

EDIT: Oh, forgot about the comments! if you have brackets in the comments in the body of the method, your count may go wrong. Therefore, you need to remove all comments from the source as your first step.

+2
source

In general, the problem you are trying to solve is to parse the C # code in the same way as the compiler, and then save the contents of the functions, rather than generate the code. So, as a background for your solution, you should look at C # grammars and how to analyze them.

According to StingyJack, a simple way to do this would be to create a regular expression that only identifies function definitions. Then you can assume that everything in between is the body of the function. However, this assumption will not handle things like several classes in one file or even the final one} at the end of the class. To deal with such things, you will have to develop a C # compiler, since processing the full C # grammar is the only thing that will correctly identify what C # considers a function.

+1
source

after you find the function headers, count the number of curly braces until you find the end of the function.

0
source

A better option would be a custom parser. As Blair said in a comment on StingyJack, it is very difficult to parse regular expression code. I tried to do this once and although balancing braces can be matched with NET, speeding up comments and lines is much more complicated.

A parser should make things a lot easier. See antlr for a good generator.

Regarding reflection, I believe that you could try to compile the code (if you have all the necessary dependencies), and then access its Reflector-like content. But I would go with a parser.

0
source

NRefactory is a tool for this job. Have a look here: http://laputa.sharpdevelop.net/content/binary/NRefactory.wmv

0
source

All Articles