473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help wanted: Generate code from delegate

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
Nov 21 '06 #1
5 1497
Hi,

What do you mean by "generate the code that the delegates describe"?

--
Dave Sexton

"mortb" <mo***@nospam.nospamwrote in message
news:eX**************@TK2MSFTNGP02.phx.gbl...
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

Nov 21 '06 #2
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
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eC**************@TK2MSFTNGP04.phx.gbl...
Hi,

What do you mean by "generate the code that the delegates describe"?

--
Dave Sexton

"mortb" <mo***@nospam.nospamwrote in message
news:eX**************@TK2MSFTNGP02.phx.gbl...
>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


Nov 21 '06 #3
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" <mo***@nospam.nospamwrote in message
news:uA*************@TK2MSFTNGP03.phx.gbl...
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
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eC**************@TK2MSFTNGP04.phx.gbl...
>Hi,

What do you mean by "generate the code that the delegates describe"?

--
Dave Sexton

"mortb" <mo***@nospam.nospamwrote in message
news:eX**************@TK2MSFTNGP02.phx.gbl...
>>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



Nov 21 '06 #4
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

"mortb" wrote:
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
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eC**************@TK2MSFTNGP04.phx.gbl...
Hi,

What do you mean by "generate the code that the delegates describe"?

--
Dave Sexton

"mortb" <mo***@nospam.nospamwrote in message
news:eX**************@TK2MSFTNGP02.phx.gbl...
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


Nov 21 '06 #5
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" <dave@jwa[remove.this]online.comwrote in message
news:ut**************@TK2MSFTNGP03.phx.gbl...
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" <mo***@nospam.nospamwrote in message
news:uA*************@TK2MSFTNGP03.phx.gbl...
>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
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eC**************@TK2MSFTNGP04.phx.gbl...
>>Hi,

What do you mean by "generate the code that the delegates describe"?

--
Dave Sexton

"mortb" <mo***@nospam.nospamwrote in message
news:eX**************@TK2MSFTNGP02.phx.gbl...
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



Nov 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
3
by: Michael Rockwell | last post by:
I am new to using C# generics and I am liking what I am finding. However the examples in online help are lacking. Can someone help me with the FindAll method of the generic List class? As I...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
0
by: john | last post by:
Hi,All MS Tech Gurus: I need your help!!! It is the second time I post this message, I need get some feedback ASAP, Please Help!! Thanks a lot in advance. John I have a csharp method, using...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
1
by: Niels Ull | last post by:
Hi! I'm using .Net 2.0 and C#, and I'm trying to generate code for build time AOP. But I cannot find out how to generate an anonymous delegate. E.g. generating code like this: class...
1
by: Simon Woods | last post by:
Hi I'm trying to generate a simple evaluator-type app. I want to generate a list of operators and say how each is to be evaluated. So, e.g., I want to add to a list of operators ...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.