DynamicMethod — Now as soon as I close my gaping jaw…

August 16, 2006 on 4:45 pm | In .NET Coding |

Ok, this .net stuff actually still gets better. I’m sitting here strugling with ways to access methods and properties dynamically without using reflection every time, and I was starting to get creative. Of course I knew the right way would be to emit some IL and just suck it up already, but setting up IL emit you have to create the assembly, the module, and the app domain… blah blah… or so I thought. Then I somehow stumbled on Lightweight Code generation. 50-100 times faster than reflection (depending on the method) and only 1.5 - 3 times speed than direct method access… yes you read that right.
I’m not going to pretend to be an expert or anything, but I am going to pretend to be a guy who’s read a lot about it now. Here’s some links for you to do the same.

This guy in particular has the whole thing pretty much mastered. It’s the most complete example I’ve seen, and basically, there isn’t much need for anything else (unless you insist on emitting more IL than necessary because you like stack based programming for some reason.)

What I’m using this thing to do is couple it with custom code attributes to markup shell classes and generate class wrappers that have the ability to write them to and read them from relavance without a single bit of extra code to write. Just straight up reflection to generate some cached IL calls that we zip through to do the writes. Basically the IL is only to call other methods that I’ve already written, but it’s ‘dynamic’ so that I can say store it in a dictionary by a string key, and call it to do some mapping for output, or I can put the delegates in a list and enumerate the list invoking each delegate, each writing a property at a time to the base. It emulates streams a bit. Sure the delegates have the tiniest bit of overhead, but it sure beats reflection and some other form of more arcane representaion of meta data (like xml files, that you’d have to put a path into some other config file, and make sure they’re available to the library at directory X blah blah blah)

Here’s another one: http://robgarrett.com/cs/blogs/software/archive/2005/10/12/1655.aspx

And Don’t forget CodeProject (the best overall site in the world for me — usually not so in depth, but they’re almost always enough to get me started in what I need to learn or do)
Here’s a couple I found out there:

http://www.codeproject.com/csharp/FastMethodInvoker.asp

http://www.codeproject.com/useritems/Dynamic_Code_Generation.asp (herb’s picture looks like John Coltrane on the cover of Blue Train — it actually may be the album cover, I can’t quite tell.)

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^