Friday, October 06, 2006


Use Token Handle Resolution API to get the Metadata for Reflection in .NET 2.0

One of the most common uses of reflection is in the use of plug-ins, such as toolbars or hosting third party plug-in functionality. A developer I worked with recently was unaware of the appropriate uses of reflection (let’s call him Bugsy!). I’d like to direct him to the words of Joel Pobar, who you might say is an expert on this topic (Joel is a Program Manager on the common language runtime (CLR) team at Microsoft, at least he was till last week!):

When Should You Use Reflection?
You should always think carefully about how you're using reflection. Using reflection occasionally without enforcing strict performance criteria is probably fine. If reflection APIs are only invoked when you're calling the part of your app that loads and invokes a third-party plug-in, then the cost should be reasonable. However, if your scenario involves a high-volume ASP.NET Web site that requires good throughput and response times, and if it makes significant use of the heavy reflection APIs in your "fast path" (the code in the application that must run very fast and is used repeatedly), you should really consider an architectural review to decide if you've made the right decisions on your use of reflection.

Where was he using reflection? Between the business rule and data access layers in an ASP.NET 2.0 financial application! (and no, he seemingly had not heard of code generation, despite several attempts to enlighten him). That architectural review sounds appropriate…

Relative performance of Invocation Mechanisms (image from MSDN article)

Scott Hanselman has a Hanselminutes webcast on the subject here.

How can you improve the speed of reflection based calls? J.D. Meier’s post details the use and the performance benefits of the new .NET 2.0 reflection API:

“RuntimeMethodHandle works approximately twice faster than compared to equivalent GetXxx API call, if the MemberInfo is not present in the back-end .NET cache”

To round off this post, here’s yet another free resource from Microsoft “Improving .NET Application Performance and Scalability” (all 1150 pages of it!)

"Information is segmented by roles, including architects, developers, testers, and administrators, to make it more relevant and actionable. This guide provides processes and actionable steps for modeling performance, measuring, testing, and tuning your applications."
By complete coincidence, I’ve been talking to a colleague about .NET performance tuning and I’m hoping to put an article together in the near future, so stay tuned.


Powered by Blogger