コード例 #1
0
        private static FastHandler GetConstructorInvoker(ConstructorInfo constructor)
        {
            // 定义一个没有名字的动态方法。
            // 关联到模块,并且跳过JIT可见性检查,可以访问所有类型的所有成员
            DynamicMethod dynamicMethod = new DynamicMethod(String.Empty, typeof(Object), new Type[] { typeof(Object[]) }, constructor.DeclaringType.Module, true);
            ILGenerator   il            = dynamicMethod.GetILGenerator();

            EmitHelper help   = new EmitHelper(il);
            Type       target = constructor.DeclaringType;

            if (target.IsValueType)
            {
                help.NewValueType(target).BoxIfValueType(target).Ret();
            }
            else if (target.IsArray)
            {
                help.PushParams(0, new Type[] { typeof(Int32) }).NewArray(target.GetElementType()).Ret();
            }
            else
            {
                help.PushParams(0, constructor).NewObj(constructor).Ret();
            }

            return((FastHandler)dynamicMethod.CreateDelegate(typeof(FastHandler)));
        }
コード例 #2
0
ファイル: TypeX.cs プロジェクト: chenmsg/sujin
        private static FastHandler GetConstructorInvoker(Type target, ConstructorInfo constructor)
        {
            // 定义一个没有名字的动态方法。
            // 关联到模块,并且跳过JIT可见性检查,可以访问所有类型的所有成员
            DynamicMethod dynamicMethod = new DynamicMethod(String.Empty, typeof(Object), new Type[] { typeof(Object[]) }, target.Module, true);

            {
                ILGenerator il   = dynamicMethod.GetILGenerator();
                EmitHelper  help = new EmitHelper(il);
                if (target.IsValueType)
                {
                    help.NewValueType(target).BoxIfValueType(target).Ret();
                }
                else if (target.IsArray)
                {
                    help.PushParams(0, new Type[] { typeof(Int32) }).NewArray(target.GetElementType()).Ret();
                }
                else
                {
                    help.PushParams(0, constructor).NewObj(constructor).Ret();
                }
            }
#if DEBUG
            //SaveIL(dynamicMethod, delegate(ILGenerator il)
            //     {
            //         EmitHelper help = new EmitHelper(il);
            //         if (target.IsValueType)
            //             help.NewValueType(target).BoxIfValueType(target).Ret();
            //         else if (target.IsArray)
            //             help.PushParams(0, new Type[] { typeof(Int32) }).NewArray(target.GetElementType()).Ret();
            //         else
            //             help.PushParams(0, constructor).NewObj(constructor).Ret();
            //     });
#endif

            return((FastHandler)dynamicMethod.CreateDelegate(typeof(FastHandler)));
        }
コード例 #3
0
ファイル: MethodInfoX.cs プロジェクト: chenmsg/sujin
        static void GetMethodInvoker(ILGenerator il, MethodInfo method)
        {
            EmitHelper help    = new EmitHelper(il);
            Type       retType = method.ReturnType;

            //if (!method.IsStatic) il.Emit(OpCodes.Ldarg_0);
            if (!method.IsStatic)
            {
                help.Ldarg(0).CastFromObject(method.DeclaringType);
            }

            // 方法的参数数组放在动态方法的第二位,所以是1
            help.PushParams(1, method)
            .Call(method)
            .BoxIfValueType(retType);

            //处理返回值,如果调用的方法没有返回值,则需要返回一个空
            if (retType == null || retType == typeof(void))
            {
                help.Ldnull().Ret();
            }
            else
            {
                help.Ret();
            }

            //调用目标方法
            //if (method.IsVirtual)
            //    il.EmitCall(OpCodes.Callvirt, method, null);
            //else
            //    il.EmitCall(OpCodes.Call, method, null);

            ////处理返回值
            //if (method.ReturnType == typeof(void))
            //    il.Emit(OpCodes.Ldnull);
            //else if (method.ReturnType.IsValueType)
            //    il.Emit(OpCodes.Box, method.ReturnType);

            //il.Emit(OpCodes.Ret);
        }