Ejemplo 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)));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Calls the specified generic method by name.
        /// </summary>
        /// <param name="name">The name of the method to call.</param>
        /// <param name="typeArguments">The type arguments to specialize the generic method.</param>
        /// <param name="args">Arguments to pass to the method.</param>
        /// <returns>The value returned by the specified method.</returns>
        public object CallMethodWithTypeArguments(string name, ICollection <Type> typeArguments, params object[] args)
        {
            return(ProfilerInterceptor.GuardInternal(() =>
            {
                var candidates = type.GetAllMethods()
                                 .Where(m => m.Name == name && CanCall(m, this.instance != null))
                                 .Select(m => MockingUtil.TryApplyTypeArguments(m, typeArguments.ToArray()))
                                 .Where(m => m != null)
                                 .ToArray();

                args = args ?? MockingUtil.NoObjects;
                object state;
                var method = MockingUtil.BindToMethod(MockingUtil.AllMembers,
                                                      candidates, ref args, null, null, null, out state);

                return CallInvoke(method, args);
            }));
        }