private static void EmitDynamicMethod(MethodInfo method, DynamicMethod callable)
        {
            var info = new MethodMataData(method);

            ILGenerator il = callable.GetILGenerator();

            EmitLoadParameters(info, il, 1);

            if (method.IsStatic)
            {
                il.EmitCall(OpCodes.Call, method, null);
            }
            else 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);
        }
        private static void EmitLoadParameters(MethodMataData info, ILGenerator il, int argumentArrayIndex)
        {
            if (!info.Method.IsStatic && !(info.Method is ConstructorInfo))
            {
                il.Emit(OpCodes.Ldarg_0);
                EmitHelper.UnboxOrCast(il, info.Method.DeclaringType);
            }

            for (int index = 0; index < info.Parameters.Length; index++)
            {
                EmitHelper.LoadArgument(il, argumentArrayIndex);
                EmitHelper.LoadInt(il, index);
                il.Emit(OpCodes.Ldelem_Ref);
                EmitHelper.UnboxOrCast(il, info.ParameterTypes[index]);
            }
        }
        public static ConstructorHandler CreateConstructorMethod(ConstructorInfo constructor)
        {
            DynamicMethod callable = CreateDynamicFactoryMethod();
            var           info     = new MethodMataData(constructor);

            Type        returnType = constructor.ReflectedType;
            ILGenerator il         = callable.GetILGenerator();

            EmitLoadParameters(info, il, 0);

            il.Emit(OpCodes.Newobj, constructor);

            if (info.ReturnType.IsValueType)
            {
                il.Emit(OpCodes.Box, returnType);
            }

            il.Emit(OpCodes.Ret);

            return(callable.CreateDelegate(typeof(ConstructorHandler)) as ConstructorHandler);
        }