Help wanted: Generate code from delegate

  • Thread starter Thread starter mortb
  • Start date Start date
M

mortb

Hello!

I make use of anonymous delegates in my code. For debugging purposes I'd
like to generate the code that the delegates describe. Do anyone have an
easy way to do this?

cheers,
mortb
 
Hi,

What do you mean by "generate the code that the delegates describe"?
 
I'll try to explain by an example:

I declare a and assign delegate variable:

MyDelType myDel = delgate() { doSomeStuff(); };
myDel += delegate() { doSomeOtherStuff(); };

What I would like to do is to have a method:

string generateCodeFromDel(MyDelType theDel)

....that will return something like a string containing the "program":

"doSomeStuff();
doSomeOtherStuff();"

cheers,
mortb
 
Hi,

I don't think there's an easy way to get C# code out of an anonymous method
at runtime.

First of all, you'd call GetInvocationList to get a list of all of the
delegates.
Secondly, on each Delegate in the list you'd call GetMethodBody on the
Method property.

The problem here is that the MethodBody class only gives you the ability to
get the IL as a byte[], and there is no easy way to convert that to C#,
AFAIK.
 
Thats not quite how delegates work. The delegate is like a list of functions
to call so adding one to a delegate like += doesnt append the source of the
new function to the source of the other, it adds it to the list. What you
would need to do would be to use the CodeDom to try and reverse engineer the
code of each one individually in a loop. You wont necesarily be able to get
the same code back and it may not compile due to the function duplicate
variable names etc.

Why would you need such a function as it will take a long while to write and
I cant see big benefit of automating it?

Ciaran O'Donnell
 
Hi,

And BTW the GetMethodBody method is new to the 2.0 framework. In earlier
framework versions you'd have to go Interop, I believe.

--
Dave Sexton

Dave Sexton said:
Hi,

I don't think there's an easy way to get C# code out of an anonymous
method at runtime.

First of all, you'd call GetInvocationList to get a list of all of the
delegates.
Secondly, on each Delegate in the list you'd call GetMethodBody on the
Method property.

The problem here is that the MethodBody class only gives you the ability
to get the IL as a byte[], and there is no easy way to convert that to C#,
AFAIK.

--
Dave Sexton

mortb said:
I'll try to explain by an example:

I declare a and assign delegate variable:

MyDelType myDel = delgate() { doSomeStuff(); };
myDel += delegate() { doSomeOtherStuff(); };

What I would like to do is to have a method:

string generateCodeFromDel(MyDelType theDel)

...that will return something like a string containing the "program":

"doSomeStuff();
doSomeOtherStuff();"

cheers,
mortb
 

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

Back
Top