public static ProfilerInterceptor.RefReturn <TReturn> CreateDynamicMethodInvoker <TReturn>(object target, MethodInfo method, object[] args)
        {
            if (args.Length != method.GetParameters().Length)
            {
                throw new MockException(
                          String.Format("Number of the supplied arguments does not match to the expected one in the method signature:" +
                                        " supplied '{0}', expected '{1}'", args.Length, method.GetParameters().Length));
            }

            ProfilerInterceptor.RefReturn <TReturn> @delegate =
                MockingUtil.CreateDynamicMethod <ProfilerInterceptor.RefReturn <TReturn> >(
                    il =>
            {
                // store arguments as local variables
                il.UnpackArgArray(OpCodes.Ldarg_1, method);

                // push object reference to the stack in case if instance method
                if (target != null)
                {
                    il.Emit(OpCodes.Ldarg_0);
                }

                // push stored arguments back to the stack
                il.PushArgArray(method);

                il.Emit((target != null) ? OpCodes.Callvirt : OpCodes.Call, method as MethodInfo);
                il.Emit(OpCodes.Ret);
            });

            return(@delegate);
        }
Example #2
0
        public static object CreateObject(this Type type, object[] args)
        {
            args = args ?? NoObjects;

            var constructorInfos = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            if (constructorInfos.Length == 0 || (type.IsValueType && args.Length == 0))
            {
                if (args.Length > 0)
                {
                    throw new MockException("Type has no non-default constructors.");
                }

                return(type.GetDefaultValue());
            }

            object state;
            var    ctor = (ConstructorInfo)MockingUtil.BindToMethod(MockingUtil.Default,
                                                                    constructorInfos, ref args, null, null, null, out state);

            var ctorParameters = ctor.GetParameters();

            for (int i = 0; i < ctorParameters.Length; ++i)
            {
                var paramType = ctorParameters[i].ParameterType;
                if (paramType.IsValueType && args[i] == null)
                {
                    args[i] = paramType.GetDefaultValue();
                }
                else if (args[i] != null && !paramType.IsAssignableFrom(args[i].GetType()))
                {
                    args[i] = Convert.ChangeType(args[i], paramType, System.Globalization.CultureInfo.CurrentCulture);
                }
            }

#if !PORTABLE
            var newCall = MockingUtil.CreateDynamicMethod <Func <object[], object> >(il =>
            {
                il.UnpackArgArray(OpCodes.Ldarg_0, ctor);
                il.PushArgArray(ctor);
                il.Emit(OpCodes.Newobj, ctor);
                if (type.IsValueType)
                {
                    il.Emit(OpCodes.Box, type);
                }
                il.Emit(OpCodes.Ret);
            });

            return(ProfilerInterceptor.GuardExternal(() =>
            {
                try
                {
                    return newCall(args);
                }
                catch (MemberAccessException ex)
                {
                    GC.KeepAlive(ex);
                    return MockingUtil.CreateInstance(type, args);
                }
            }));
#else
            return(MockingUtil.CreateInstance(type, args));
#endif
        }