Printing Function Code

  • Thread starter Thread starter Jeff Gaines
  • Start date Start date
J

Jeff Gaines

I'll try and press the right button this time.....

If I have a function:

private void TestFunction(int testNumber)
{
return testNumber * 5;
}

Is there a way to print the code in the function itself to a Text Box?

I am trying to get my head round CodeDom and my brain hurts, something
like the above would enable me to say 'if you do this + function code you
get this + function result.

I suspect it can't be done but would appreciate any thoughts.
 
I'll try and press the right button this time.....

If I have a function:

private void TestFunction(int testNumber)
{
return testNumber * 5;
}

Is there a way to print the code in the function itself to a Text Box?

I am trying to get my head round CodeDom and my brain hurts, something
like the above would enable me to say 'if you do this + function code you
get this + function result.

I suspect it can't be done but would appreciate any thoughts.

Hmmmm. Off the top of my head, here's how I would approach that:

- Using Reflection, load the class that has this function.
- Create a MethodInfo object for the method.
- Use CodeDom (or whatever) to take this method and turn it into C# code. (I
have absolutely no clue how to do this beyond the blanket statement I just
made, but it has to be possible since that's pretty much what Reflector
does, right?)
- Execute the method with a given argument.
- Print out the code and the result.

Please use a #2 pencil and have your answer on my desk by the end of the
week.... Seriously, let us know if you get this working, and post a working
sample. I'm sure it would benefit someone.
 
- Use CodeDom (or whatever) to take this method and turn it into C# code.
(I have absolutely no clue how to do this beyond the blanket statement I
just made, but it has to be possible since that's pretty much what
Reflector does, right?)

I take it back. The .NET Framework does not seem to have a built-in way to
perform the easy-sounding step above, i.e., the conversion of IL to C#
statements.

If you understand C++, there's source for a program called Anakrino.
http://legacy.saurik.com/net/exemplar/ Maybe you could convert it to C# and
make a really cool add-in for us all...?
 
I take it back. The .NET Framework does not seem to have a built-in way to
perform the easy-sounding step above, i.e., the conversion of IL to C#
statements.

If you understand C++, there's source for a program called Anakrino.
http://legacy.saurik.com/net/exemplar/ Maybe you could convert it to C#
and make a really cool add-in for us all...?

I'm refining my requirements a bit - what I think I want is the ability to
paste some CodeDom graph code into a text box and then hand it over to be
compiled into C# source.
I think I've got many of the components to do that, I just need to get my
head round putting them together.
I've been using something I found on the Code Project to take C# source
and turn it into a CodeDom graph using Sharp Develop's refactoring dll. It
can then take that CodeDom graph and turn it back into C# (or VB) source.
Trouble is it's not quite the same as the original.
Pity MSFT didn't build something like that into CodeDom...
 
Back
Top