Ejemplo n.º 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)));
        }
Ejemplo n.º 2
0
        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)));
        }