How to parse the names of methods & variables in a cs file

S

Simone M

I would like to make a list of all the names of methods and variables
in a cs file. I need to open up a file, create the list of all methods
and properties, and variables, contained in that file, and close the
file.

Is there any class that will help me doing that?

Cheers,
Simone
 
M

Michael Nemtsev

Hello Simone,

How would u like to do it? Statically? Use CodeSmith tool for this case

SM> I would like to make a list of all the names of methods and
SM> variables in a cs file. I need to open up a file, create the list of
SM> all methods and properties, and variables, contained in that file,
SM> and close the file.
SM>
SM> Is there any class that will help me doing that?
SM>
SM> Cheers,
SM> Simone
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
J

Jon Skeet [C# MVP]

Simone M said:
I would like to make a list of all the names of methods and variables
in a cs file. I need to open up a file, create the list of all methods
and properties, and variables, contained in that file, and close the
file.

Is there any class that will help me doing that?

Do you definitely need to do it on the C# file itself, rather than the
assembly which is produced? It's much easier to do with reflection on
the generated code than it is to effectively do a lot of the parsing
work that the C# compiler does.
 
S

Simone M

Jon,
Thanks for the tip. I thought there would be a class available to do it
directly on the cs file.
But I will give Reflection a try.
Cheers!
Simone
 
B

Bruce Wood

As well, you could look into FxCop, which validates .NET code by
examining the compiled assemblies.

FxCop allows you to add custom rules, so if you're doing this in order
to check a naming convention (for example), you could get that "for
free" by reading up on how to write custom rules in FxCop and
leveraging off that framework. They've done all the heavy lifting for
you... all you have to supply are the additional rules to check.

Of course, if you want the names for some other purpose, that may not
work for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top