Randomly choose method and extract method body

  • Thread starter Thread starter Chris Green
  • Start date Start date
C

Chris Green

I would welcome any advice someone could offer on the follow problem.
I need to code a command line utility in c sharp. The utility will
accept a single parameter; the name of a cs file (not an assembly). I
then need to pick a method at random and display the text of that
method contained in the cs file.

Initially i wanted to use reflection but because the utility looks at
a source file instead of a dll I don't think this will work,
furthermore I don't think reflection allows the extraction of a
method's body.

Regular expressions could be an option but my regular expression
skills are lacking.

Any ideas?

TIA.
 
Hi Chris,

That sound as a homework :)

You have to differenciate between a method call and a method definition,
you can do this be either check the sintaxis of the method declaration or
how the method is called, you will have to make sure that you cover all the
possibilities of either one like:
- A method definition have a type before it and a ( after it , a number of
chars and a ) then followed by a {

When you find a method definition you then start counting by { incrementing
each time you see a { and decrementing when you see a } , when the count
reach 0 you have your method body.

Please note that the above is only to give you an idea , is not the
complete thing.

Cheers,
 
I wish it was homework, however my boss wants it for a project that he
is keeping secret at the moment. Thanks for the help.
 
Back
Top