Vishful thinking…

Quick notes on Reflection performance in .NET

Posted in Uncategorized by viswaug on October 12, 2007

Here are some quick notes to keep in mind if you are using Reflection in your projects. You probably know that using Reflection takes a toll on the performance of your application. Bearing the following in mind when you are using Reflection in your projects can help you design better performing applications.

Fast and Light Functions

Costly Functions

  • typeof
  • Object.GetType
  • typeof == Object.GetType
  • Type equivalence APIs (including typehandle operator overloads)
  • get_Module
  • get_MemberType
  • Some of the IsXX predicate APIs New token/handle resolution APIs in the .NET Framework 2.0
  • GetXX APIs (MethodInfo, PropertyInfo, FieldInfo, and so on)
  • GetCustomAttributes
  • Type.InvokeMember
  • Invoke APIs (MethodInfo.Invoke, FieldInfo.GetValue, and so on)
  • get_Name (Name property)
  • Activator.CreateInstance

 

 This figure illustrates the invocation mechanisms and their performance overheads.

fig02

Type.InvokeMember is the slowest of the late-bound invocation mechanisms because there are two functions that InvokeMember needs to perform to properly invoke a member. First, it must figure out the exact member it’s supposed to invoke, as determined by the user’s string input, and then perform checks to be sure the invocation is safe.

MethodBase.Invoke, on the other hand, doesn’t need to figure out which method it must call because the MethodBase already holds the identity of the method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: