Click here to Skip to main content
11,414,561 members (66,088 online)
Click here to Skip to main content

A General Fast Method Invoker

, 4 Jul 2006
Rate this:
Please Sign up or sign in to vote.
Method reflecting invoke is nice, but very frequently it can be too slow. This article describes an alternative method for dynamic method invoke.

Sample Image - FastMethodInvoker.gif

Introduction

Sometimes, I run across the need to dynamically invoke the method of an object, where the actual method might not be known until run-time. Usually, Reflecting is nice, but frequently doing it can be too slow. This article describes an alternative method for dynamic method invocation.

Background

When I read the article Fast Dynamic Property Accessors, I was thinking about my project, it has a lots of reflecting methods in circle. But it's methods not properties. But the DynamicMethod reminded me, maybe I could use Emit to generate a DynamicMethod to bind a special method before it can be invoked. I hope it will improve performance.

Using the Code

First, I reflected out the method which will be invoked:

MethodInfo methodInfo = typeof(Person).GetMethod("Say");

Then, I get the MethodInvoker to invoke:

FastInvokeHandler fastInvoker = GetMethodInvoker(methodInfo);
fastInvoker(new Person(), new object[]{"hello"});

Instead of using reflection method, invoke in the past:

methodInfo.Invoke(new Person(), new object[]{"hello"});

Implementation

First, I need to define a delegate to adapt the dynamic method:

public delegate object FastInvokeHandler(object target, 
                                   object[] paramters);

It looks the same as the class MethodInfo's Invoke method. Yes, that means I can write the same code to use it like in the past.

This code generates the DynamicMethod:

public static FastInvokeHandler GetMethodInvoker(MethodInfo methodInfo)
{
    DynamicMethod dynamicMethod = new DynamicMethod(string.Empty, 
                     typeof(object), new Type[] { typeof(object), 
                     typeof(object[]) }, 
                     methodInfo.DeclaringType.Module);
    ILGenerator il = dynamicMethod.GetILGenerator();
    ParameterInfo[] ps = methodInfo.GetParameters();
    Type[] paramTypes = new Type[ps.Length];
    for (int i = 0; i < paramTypes.Length; i++)
    {
        paramTypes[i] = ps[i].ParameterType;
    }
    LocalBuilder[] locals = new LocalBuilder[paramTypes.Length];
    for (int i = 0; i < paramTypes.Length; i++)
    {
        locals[i] = il.DeclareLocal(paramTypes[i]);
    }
    for (int i = 0; i < paramTypes.Length; i++)
    {
        il.Emit(OpCodes.Ldarg_1);
        EmitFastInt(il, i);
        il.Emit(OpCodes.Ldelem_Ref);
        EmitCastToReference(il, paramTypes[i]);
        il.Emit(OpCodes.Stloc, locals[i]);
    }
    il.Emit(OpCodes.Ldarg_0);
    for (int i = 0; i < paramTypes.Length; i++)
    {
        il.Emit(OpCodes.Ldloc, locals[i]);
    }
    il.EmitCall(OpCodes.Call, methodInfo, null);
    if (methodInfo.ReturnType == typeof(void))
        il.Emit(OpCodes.Ldnull);
    else
        EmitBoxIfNeeded(il, methodInfo.ReturnType);
    il.Emit(OpCodes.Ret);
    FastInvokeHandler invoder = 
      (FastInvokeHandler)dynamicMethod.CreateDelegate(
      typeof(FastInvokeHandler));
    return invoder;
}

Conclusion

Well, I think this is a general way that can be used instead of most of the reflection methods to get about 50 times performance improvement. Any suggestions for improvements are welcome.

Extra advantage (reminded by MaxGuernsey): If an exception occurs in your code, FastInovker would throw the original one, but the Method.Invoke would throw a TargetInvocationException.

History

  • 2006-7-05: Updated to add static method support. Thanks Manuel Abadia.
  • 2006-6-30: Updated to add ref/out parameter support. Thanks Roger for his nice suggestion.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Luyan

China China
I am currently working for a .NET framework names AgileFramework. It's introduction at here:
http://www.agilelabs.cn/agileframework

Now I'm living in China. I have been designing and developing .NET based software applications for 5+ years.

Comments and Discussions

 
QuestionNot necessay at all Pin
FatCatProgrammer22-Jan-13 9:25
memberFatCatProgrammer22-Jan-13 9:25 
AnswerRe: Not necessay at all Pin
Lu Jing9-Jun-13 21:06
memberLu Jing9-Jun-13 21:06 
AnswerRe: Not necessay at all Pin
cajoncillos1-Jul-14 13:24
membercajoncillos1-Jul-14 13:24 
GeneralRe: Not necessay at all Pin
FatCatProgrammer1-Jul-14 17:10
memberFatCatProgrammer1-Jul-14 17:10 
Questionabout your code's license Pin
qsmy12322-Aug-12 16:00
memberqsmy12322-Aug-12 16:00 
QuestionLicense Pin
Skeletor2323-Mar-12 6:05
memberSkeletor2323-Mar-12 6:05 
QuestionConstructorInfo Support. Pin
Member 455125623-Jun-10 8:29
memberMember 455125623-Jun-10 8:29 
AnswerRe: ConstructorInfo Support. Pin
Member 455125623-Jun-10 10:00
memberMember 455125623-Jun-10 10:00 
QuestionCan't work with 'struct' ? Pin
qmxle1-Oct-09 6:11
memberqmxle1-Oct-09 6:11 
GeneralExcellent! Accessibility fix possible. Pin
black198122-Jul-09 3:49
memberblack198122-Jul-09 3:49 
QuestionPartial Trust question Pin
Andrey Belykh25-Apr-08 7:57
memberAndrey Belykh25-Apr-08 7:57 
Generalprivate member supported and easier way with better performance Pin
yzh_x1-Jul-07 20:38
memberyzh_x1-Jul-07 20:38 
GeneralRe: private member supported and easier way with better performance Pin
Linguar2-Jul-07 10:15
memberLinguar2-Jul-07 10:15 
GeneralRe: private member supported and easier way with better performance Pin
yzh_x2-Jul-07 17:44
memberyzh_x2-Jul-07 17:44 
GeneralRe: private member supported and easier way with better performance Pin
Linguar2-Jul-07 19:26
memberLinguar2-Jul-07 19:26 
GeneralWrapper Pin
Mick O'Neill7-Jun-07 14:46
memberMick O'Neill7-Jun-07 14:46 
GeneralRe: Wrapper Pin
johnnycrash9917-May-09 10:03
memberjohnnycrash9917-May-09 10:03 
GeneralComments and structure. Pin
Linguar19-Mar-07 7:56
memberLinguar19-Mar-07 7:56 
GeneralRe: Comments and structure. Pin
Luyan24-Mar-07 3:35
memberLuyan24-Mar-07 3:35 
GeneralA Wrapper class for your code Pin
billy p6-Oct-06 10:35
memberbilly p6-Oct-06 10:35 
GeneralI think the result is not exact Pin
happyhippyie3-Sep-06 1:10
memberhappyhippyie3-Sep-06 1:10 
GeneralRe: I think the result is not exact Pin
ueqt20071-Sep-07 20:42
memberueqt20071-Sep-07 20:42 
AnswerRe: I think the result is not exact Pin
xx91819-Jan-08 4:29
memberxx91819-Jan-08 4:29 
QuestionMethodAccessException unhandled when using generic string types Pin
canerten9-Aug-06 3:20
membercanerten9-Aug-06 3:20 
QuestionRe: MethodAccessException unhandled when using generic string types Pin
canerten9-Aug-06 3:47
membercanerten9-Aug-06 3:47 
Questionany suggestion about 1.1 Pin
aibo8-Aug-06 1:46
memberaibo8-Aug-06 1:46 
QuestionSpeed Difference ? Pin
Greg Roberts25-Jul-06 23:18
memberGreg Roberts25-Jul-06 23:18 
AnswerRe: Speed Difference ? Pin
Linguar19-Mar-07 14:23
memberLinguar19-Mar-07 14:23 
GeneralRe: Speed Difference ? Pin
wildcatsoft14-Aug-08 21:48
memberwildcatsoft14-Aug-08 21:48 
GeneralRe: Speed Difference ? Pin
colinmeier2-Mar-11 11:00
membercolinmeier2-Mar-11 11:00 
GeneralRe: Speed Difference ? Pin
Kerem Guemruekcue5-Dec-12 8:21
memberKerem Guemruekcue5-Dec-12 8:21 
GeneralNice Pin
mikeperetz20-Jul-06 18:36
membermikeperetz20-Jul-06 18:36 
QuestionHow do you "invoke in the past"? Pin
zxcv123412341234123420-Jul-06 5:13
memberzxcv123412341234123420-Jul-06 5:13 
QuestionNewbie to Reflection Pin
shahp11-Jul-06 7:38
membershahp11-Jul-06 7:38 
AnswerRe: Newbie to Reflection Pin
Luyan11-Jul-06 23:31
memberLuyan11-Jul-06 23:31 
QuestionHow about private member call? Pin
gattazzo19745-Jul-06 2:17
membergattazzo19745-Jul-06 2:17 
AnswerRe: How about private member call? Pin
Stephen Erisman5-Jul-06 5:51
memberStephen Erisman5-Jul-06 5:51 
GeneralRe: How about private member call? Pin
gattazzo19745-Jul-06 7:25
membergattazzo19745-Jul-06 7:25 
GeneralRe: How about private member call? Pin
Roger J5-Jul-06 21:41
memberRoger J5-Jul-06 21:41 
GeneralRe: How about private member call? Pin
gattazzo19746-Jul-06 8:16
membergattazzo19746-Jul-06 8:16 
QuestionInteresting Pin
mgungora4-Jul-06 10:21
membermgungora4-Jul-06 10:21 
QuestionSource code use Pin
Manuel Abadia4-Jul-06 2:20
memberManuel Abadia4-Jul-06 2:20 
AnswerRe: Source code use [modified] Pin
Luyan4-Jul-06 3:19
memberLuyan4-Jul-06 3:19 
QuestionRe: Source code use [modified] Pin
Manuel Abadia4-Jul-06 7:00
memberManuel Abadia4-Jul-06 7:00 
AnswerRe: Source code use Pin
Luyan5-Jul-06 5:48
memberLuyan5-Jul-06 5:48 
GeneralAgreed. Five out of five. Pin
MaxGuernsey3-Jul-06 19:27
memberMaxGuernsey3-Jul-06 19:27 
GeneralRe: Agreed. Five out of five. Pin
Luyan3-Jul-06 23:44
memberLuyan3-Jul-06 23:44 
GeneralA Better Method Pin
Stephen Erisman3-Jul-06 19:10
memberStephen Erisman3-Jul-06 19:10 
GeneralRe: A Better Method [modified] Pin
Luyan3-Jul-06 23:34
memberLuyan3-Jul-06 23:34 
QuestionRe: A Better Method Pin
Stephen Erisman5-Jul-06 6:05
memberStephen Erisman5-Jul-06 6:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150427.4 | Last Updated 4 Jul 2006
Article Copyright 2006 by Luyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid