Architecture - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Architecture arrow Page 3 - Emitting and Debugging MSIL
Emitting and Debugging MSIL
By Paul Kimmel

Rate This Article: Add This Article To:

Emitting and Debugging MSIL - Scenarios
( Page 3 of 3 )

Scenarios for Using Emitted Code

Emitted code is arguably a very advanced technique. A prudent question is why you would ever want to know that or use that. Besides wowing your friends with intellectual playground machismo, compiler writers would want to know those things. However, in truth it is hard to find every day scenarios for writing emitters. The first problem is that it will probably take you ten times longer to write an emitter than regular code, but there are a couple scenarios for writing emitters.

ADVERTISEMENT

Aside from compiler writers, suppose you have a general technique that uses reflection. Well, reflection is slow. What you could do instead is write an emitter that replaces the reflected code with emitted literal code. The emitted code would be generated by reflection but run as MSIL, so once emitted it might run many times faster than the reflection code. In practice I wrote a reflection tool that dumps an object state, like the ObjectDumper utility. I then re-wrote the dumper as an emitter that emitted MSIL for each specific object property. The emitter did the same job as the reflection tool and ran about six times faster. The downside, it took me about ten hours to write the emitter. (Now you know how I spent my weekend.)

Summary

Exploring Reflection.Emit will give you intimate insight as to how compilers take code like C# and convert it to MSIL (Microsoft Intermediate Language) code. In you explorations you might stumble upon cool framework elements like ISymbolDocumentWriter that lets you preserve symbol information much as is stored in .PDB (debug) file.

Practical uses for emitter a relatively few. For example, compiler writers need to know this stuff, and if you are using reflection then you could get better performance out of emitted code but it’s going to take you some time to get the emitter right.

Personally I learn this stuff ‘cause it feels akin to having super-hero powers.

[Disclaimer: All super powers are used for good and to thwart evil doers. You will probably get further ahead in your career by a little smart and a lot nice than you will by learning to bend spoons with your mind, but bending spoons is cool at parties.]

Biography

Paul Kimmel is the VB Today columnist for HYPERLINK "http://www.codeguru.com/" \t "new" www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his book LINQ Unleashed for C#. It is currently available online at safari.informit.com as a rough cut (digital PDF format). Paul is an application architect for Electronic Data Systems in Lansing, Michigan. You may contact him for technology questions at pkimmel@softconcepts.com.

If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.



 
 
>>> More Architecture Articles          >>> More By Paul Kimmel
 



HD VOIP Has Arrived (with Tony Konstner)

Play Video >

All Videos >

Google and blonde jokes?

Read now >

Favorite books!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.