/// <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 && MockingUtil.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); })); }
/// <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 && MockingUtil.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); })); }
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)); }