来给反射调用加加速(2):手写Emit

有个库,人家写好了,呵呵,咱直接调用就行了,具体怎么实现的我不清楚啊。。

 

分享一些我封装好的扩展方法吧,基于MethodInfo的,名叫FastInvoke

 

 

 

public static void FastInvoke(this MethodInfo methodInfo, object target)

        {

            Dynamic<object>.Instance.Procedure.Explicit.CreateDelegate(methodInfo)(target);

        }



        public static void FastInvoke<TArg1>(this MethodInfo methodInfo, object target, TArg1 arg1)

        {

            Dynamic<object>.Instance.Procedure.Explicit<TArg1>.CreateDelegate(methodInfo)(target, arg1);

        }



        public static void FastInvoke<TArg1, TArg2>(this MethodInfo methodInfo, object target, TArg1 arg1, TArg2 arg2)

        {

            Dynamic<object>.Instance.Procedure.Explicit<TArg1, TArg2>.CreateDelegate(methodInfo)(target, arg1, arg2);

        }



        public static void FastInvoke<TArg1, TArg2, TArg3>(this MethodInfo methodInfo, object target, TArg1 arg1, TArg2 arg2, TArg3 arg3)

        {

            Dynamic<object>.Instance.Procedure.Explicit<TArg1, TArg2, TArg3>.CreateDelegate(methodInfo)(target, arg1, arg2, arg3);

        }



        public static void FastInvoke<TArg1, TArg2, TArg3, TArg4>(this MethodInfo methodInfo, object target, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4)

        {

            Dynamic<object>.Instance.Procedure.Explicit<TArg1, TArg2, TArg3, TArg4>.CreateDelegate(methodInfo)(target, arg1, arg2, arg3, arg4);

        }



        public static void FastInvoke<TArg1, TArg2, TArg3, TArg4, TArg5>(this MethodInfo methodInfo, object target, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, TArg5 arg5)

        {

            Dynamic<object>.Instance.Procedure.Explicit<TArg1, TArg2, TArg3, TArg4, TArg5>.CreateDelegate(methodInfo)(target, arg1, arg2, arg3, arg4, arg5);

        }



        public static TRet FastInvoke<TRet>(this MethodInfo methodInfo, object target)

        {

            return Dynamic<object>.Instance.Function<TRet>.Explicit.CreateDelegate(methodInfo)(target);

        }



        public static TRet FastInvoke<TRet, TArg1>(this MethodInfo methodInfo, object target, TArg1 arg1)

        {

            return Dynamic<object>.Instance.Function<TRet>.Explicit<TArg1>.CreateDelegate(methodInfo)(target, arg1);

        }



        public static TRet FastInvoke<TRet, TArg1, TArg2>(this MethodInfo methodInfo, object target, TArg1 arg1, TArg2 arg2)

        {

            return Dynamic<object>.Instance.Function<TRet>.Explicit<TArg1, TArg2>.CreateDelegate(methodInfo)(target, arg1, arg2);

        }



        public static TRet FastInvoke<TRet, TArg1, TArg2, TArg3>(this MethodInfo methodInfo, object target, TArg1 arg1, TArg2 arg2, TArg3 arg3)

        {

            return Dynamic<object>.Instance.Function<TRet>.Explicit<TArg1, TArg2, TArg3>.CreateDelegate(methodInfo)(target, arg1, arg2, arg3);

        }



        public static TRet FastInvoke<TRet, TArg1, TArg2, TArg3, TArg4>(this MethodInfo methodInfo, object target, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4)

        {

            return Dynamic<object>.Instance.Function<TRet>.Explicit<TArg1, TArg2, TArg3, TArg4>.CreateDelegate(methodInfo)(target, arg1, arg2, arg3, arg4);

        }



        public static TRet FastInvoke<TRet, TArg1, TArg2, TArg3, TArg4, Targ5>(this MethodInfo methodInfo, object target, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4, Targ5 arg5)

        {

            return Dynamic<object>.Instance.Function<TRet>.Explicit<TArg1, TArg2, TArg3, TArg4, Targ5>.CreateDelegate(methodInfo)(target, arg1, arg2, arg3, arg4, arg5);

        }

那个DLL地址:http://files.cnblogs.com/prime/DynamicUtilities.rar

你可能感兴趣的:(反射)