Geeks With Blogs

News Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009

I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta an ASP.NET MVC application written in C#.NET.

Some of My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Shahed Khan blog

Some tips to keep in mind when using reflection....
Source MSDN:

Improving Reflection Performance

There are various reasons why an application can't define a static extensibility contract, but it's usually a result of the flexibility the architect built in. Here are some ways to reduce some of the cost of reflection and of the MemberInfo cache.

Avoid using Type.InvokeMember to call late-bound methods  Type.InvokeMember is the slowest way to invoke a member via reflection. If your scenario doesn't involve COM interop invocation (something which Type.InvokeMember makes easier), consider other alternatives for setting up calls to members obtained through late-bound methods.

Avoid case-insensitive member lookups  When calling both the plural and non-plural GetXX APIs, avoid specifying BindingFlags.IgnoreCase as a binding flag argument as it has a significant invocation and working set costs. Metadata is case sensitive and to perform an "ignore case" operation, reflection needs to obtain the metadata string names for all members of a type and then do a case-insensitive comparison. In addition, due to the design of the reflection cache, a second MemberInfo cache is created for the case-insensitive members, which basically doubles an application's working set.

Use BindingFlags.ExactMatch whenever possible  Specify this BindingFlag when calling MethodBase.Invoke if you know that the types of the arguments you are passing exactly match the types of the method's parameters. It will allow reflection to go down an optimized, more efficient code path that does not perform argument type-coercion. If the argument types don't match the method's parameter types, an exception will be thrown.

Call the non-plural GetXX method if you know the name of the instance you care about (.NET Framework 2.0 only)  The MemberInfo cache is lazily populated in the .NET Framework 2.0, which means lower working set cost, and less time to retrieve the method. If you know the name of the particular method you want to obtain, use the non-plural GetXX method.

Cache only the handle to a member (.NET Framework 2.0 only)  In the .NET Framework 2.0, developers have the ability to define a cache policy themselves. The .NET Framework 2.0 introduces new APIs called the token handle resolution APIs. This set of APIs exposes some of the fundamental member identity concepts of the runtime and allows a user to set up and execute a MemberInfo caching policy on their own.

Posted on Monday, January 22, 2007 11:31 PM | Back to top

Comments on this post: Improving Reflection Performance

# re: Improving Reflection Performance
Requesting Gravatar...
I read a lot about reflection and all articles says that reflection more slower than direct calls.( Minimum 4 times )

İ am developing a win32 GUI ( C# ) and at main form there is linklabels .When you click linklabels usercontrols loading into i panel .All pages ( user operations ) are usercontrol so when user want to open another page i am removing the current usercontrol and adding desired usercontrol in to a panel.

If i want to extend GUI i have to build Mainform everytime .But i thought if i use reflection and convert every usercontrol to dll .I can call them via latebinding iso with late bindind i can store page information in dll and i dont have to build Mainform everytime just build one usercontrol project ,make dll and update Mainform automatically.

But i read a lot of artcile and all says reflection is very slow .Tomy usercontrol just call Ctor() after that Call LoadPage() and Cast object as a usercontrol and add it to a panel
And my question is after dll (usercontrol) adding if user hit the button in usercontrol is that function for example Button1_Click() will be more slower than direct call (old one).
Sorry about my pure English but At the end of artcile MSDN said you can reach him via your email :)

---This Code is not working just summarize---

// when LİNKLABEL clicked at Mainform

Type[] types = operationList[i].OperationAssembly.GetTypes();
foreach (Type typ in types)
ConstructorInfo[] infos = typ.GetConstructors();
objUserControl = infos[0].Invoke(
object result = typ.InvokeMember("LoadPage",BindingFlags.Default | BindingFlags.InvokeMethod,null,objUserControl,null);
UserControl us = (objUserControl as UserControl);
//Remving old Usercontrol from panel and adding us into a panel as a control
//I know this Invoke Member is slow and has performance improvement tricks like caching

// I can Cache LoadPage Method with CacheKey Class
class CacheKey
public CacheKey(RuntimeTypeHandle th, string methodName, Type[] args)
InstanceHandle = th;
MethodName = methodName;
TypeArguments = args;

LETS ASSUME IN USERCONTROL i have private method Button1_Click()

How can i cache private Methods in Usercontrol For example how can i cache Button1_Click in UserControl ???????
//SO only LoadPage () will be fast !! IS it Correct
Left by Serhat Şahin on Jul 13, 2008 2:00 AM

Your comment:
 (will show your gravatar)

Copyright © Shahed Khan | Powered by: