I'm brushing up on some of the finer points of c# at the moment, reading "Programming C#" by Jesse Liberty, it's full of really neat stuff.
The author has a good chapter on Reflection, and specifically emitting op codes to dynamically create an IL class.
Obviously the performance on this is fantastic, but I wonder, as a business logic / web apps developer, is there any use in this technique for me?
I'd love to take a crack at it but in traditional n-tier, business apps what reason would I have?
If you mean a business reason to use dynamic compilation in general, we definitely had one.
In a large pricing application, users had the need to write formulas to generate prices based on certain base prices, prices of other related products, and a set of inbuilt functions reflecting different pricing strategies.
We did this using CodeDOM, and created a C# class based on what the users entered for each formula, one function per formula. The syntax they used was a slightly modified C# syntax, which we knocked into shape to get it to compile. This was much much quicker
than interpreting and parsing the formulas ourselves. The prices of 100s of thousands of products were then calculated based on these formulas, so it made a lot of sense to have them compiled.
But if you mean specifically a reason for creating a class based on IL opcodes at runtime, I'm not sure. If the users had wanted some totally new expression syntax (like APL maybe) which was unlike any language supported by CodeDOM, maybe it would have made
sense to have a mini compiler of our own, then emit the IL code dynamically.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.