Exemplo n.º 1
0
        public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
        {
            var wrapper   = this.Value as ExpressionContainer;
            var valueExpr = wrapper.Expression;

            var typeArgs = MockingUtil.TryGetTypeArgumentsFromBinder(binder);

            var candidateMethods = valueExpr.Type.GetAllMethods()
                                   .Where(m => MockingUtil.StringEqual(m.Name, binder.Name, binder.IgnoreCase) && m.IsStatic == wrapper.IsStatic)
                                   .Where(m => m.GetParameters().Length >= args.Length)
                                   .Select(m =>
            {
                if (typeArgs == null)
                {
                    return(MockingUtil.TrySpecializeGenericMethod(m, args.Select(a => a.RuntimeType).ToArray()) ?? m);
                }
                else
                {
                    return(MockingUtil.TryApplyTypeArguments(m, typeArgs));
                }
            })
                                   .Where(m => m != null)
                                   .Where(m =>
            {
                var methodParams = m.GetParameters();
                for (int i = 0; i < args.Length; ++i)
                {
                    var matcher = UnwrapMatcher(args[i]);
                    if (matcher != null)
                    {
                        var argType = matcher.ReturnType;
                        if (!methodParams[i].ParameterType.IsAssignableFrom(argType))
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            })
                                   .ToArray();

            if (candidateMethods.Length == 0 && args.Length == 0)
            {
                return(DoBindGetMember(binder.ReturnType, binder.Name, binder.IgnoreCase));
            }

            var methodArgs = args.Select(a =>
            {
                var matcher = UnwrapMatcher(a);
                return(matcher != null ? matcher.ReturnType.GetDefaultValue() : a.Value);
            }).ToArray();

            object state;
            var    method = (MethodInfo)MockingUtil.BindToMethod(MockingUtil.Default, candidateMethods, ref methodArgs, null, null, null, out state);

            var memberExpr = Expression.Call(!wrapper.IsStatic ? valueExpr : null, method, args.Select(FromArg).ToArray());

            return(CreateRecorder(memberExpr, binder.ReturnType, BindingRestrictions.GetInstanceRestriction(this.Expression, this.Value)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Calls the specified method by name.
        /// </summary>
        /// <param name="name">The name of the method to call.</param>
        /// <param name="args">Arguments to pass to the method.</param>
        /// <returns>The value returned by the specified method.</returns>
        public object CallMethod(string name, params object[] args)
        {
            return(ProfilerInterceptor.GuardInternal(() =>
            {
                args = args ?? MockingUtil.NoObjects;
                var candidates = type.GetAllMethods()
                                 .Where(m => m.Name == name && CanCall(m, this.instance != null))
                                 .Select(m => MockingUtil.TrySpecializeGenericMethod(m, args.Select(a => a != null ? a.GetType() : null).ToArray()) ?? m)
                                 .ToArray();
                object state;
                var method = MockingUtil.BindToMethod(MockingUtil.AllMembers,
                                                      candidates, ref args, null, null, null, out state);

                return CallInvoke(method, args);
            }));
        }
Exemplo n.º 3
0
        private object CallMethodInternal(string name, ICollection <Type> argTypes, ParameterModifier[] argModifiers, object[] argValues)
        {
            if (argTypes == null)
            {
                throw new ArgumentNullException("argTypes");
            }

            if (argValues == null || argValues.Length == 0)
            {
                argValues = argTypes.Select(t => MockingUtil.GetDefaultValue(t)).ToArray();
            }

            if (argTypes.Count != argValues.Length)
            {
                throw new ArgumentException("The number of argument types does not match the number of argument values");
            }

            var argValueIndex = 0;

            foreach (var argType in argTypes)
            {
                if (argValues[argValueIndex] != null && argType != argValues[argValueIndex].GetType() ||
                    argValues[argValueIndex] == null && argValues[argValueIndex] != MockingUtil.GetDefaultValue(argType))
                {
                    throw new ArgumentException("One or more arguments types does not match the argument values");
                }
                argValueIndex++;
            }

            var candidates = type.GetAllMethods()
                             .Where(m => m.Name == name && MockingUtil.CanCall(m, this.instance != null))
                             .Select(m => MockingUtil.TrySpecializeGenericMethod(m, argTypes.ToArray()) ?? m)
                             .ToArray();

            var method = MockingUtil.SelectMethod(MockingUtil.AllMembers, candidates, argTypes.ToArray(), argModifiers);

            return(CallInvoke(method, argValues));
        }