Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Get Firefox

Locations of visitors to this page

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

Fast Generic Dynamic Property Access with C# - Now With 10% More Awesome!

After reading James Nies's great piece, Fast Dynamic Property Access with C#, which gives a great performance alternative for repetative reflection tasks, I started reading the comments and found two forks from it, one for more than just properties by Seth Heeren and the other being an implementation that supports generics (and sports better performance, of course) by Tobias Hertkorn, all of the above being excellent work.
These, in turn, got me to start thinking there had to be a better way to create the dynamic assembly than having to hack pieces of IL, so I just decided to generate a CodeDom graph on the fly.
This is my first time creating on-the-fly assemblies and it really wasn't so difficult - just a matter of setting the CompilerParameters.GenerateInMemory property to true.

Although it's more maintainable and is less likely to break in future CLR versions, it's slower than the previous version, given the fact that there's a compiler involved, but once you get over that hurdle (say, by caching the original object), it's about the same if not better.
Here is the source of the modified GenericPropertyAccessor.cs, but the rest you need to download from the previous article.

Last bit: During work on my code, I've come across yet another bug in CodeDom, "The C# CodeDom provider does not work correctly with PrivateImplementationType with TypeArguments", which has been flagged as fixed in the next version. Another bug in CodeDom - who'd've thunk?

Comments

Jason Haley said:

# June 22, 2006 10:40 AM

Seth said:

I noticed that people still link to the FastDynamicMemberAccessor.zip file so I found the file and put it up again.

Just for future's sake following url would probably work longer:

downloads.sehe.nl/FastDynamicMemberAccessor.zip

# August 15, 2007 7:40 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)