private void InvokeBaseIntercept(BaseInvocation bi, MethodInfo call, object mockObject, params object[] args) { bi.ArgumentHash = this.HashAggregate(args); //push expected invocation to the stack for this thread this.baseInvocations.AddOrUpdate(System.Threading.Thread.CurrentThread, bi, (th, s1) => bi); object ret = call.Invoke(mockObject, args); if (!Equals(ret, bi.BaseInvocationResult.ReturnValue)) throw new Exception(string.Format("Error invoking base, Result {0} is not equal to returned value {1}", bi.BaseInvocationResult.ReturnValue, ret)); }
public object InvokeBase(LambdaExpression expression, MethodInfo method, object mockObject) { var bi = new BaseInvocation { Method = method, BaseInvocationResult = null }; System.Diagnostics.Debug.WriteLine("Original Expression: " + expression); //refactor method call to call into our intercepting invoker var call = (MethodCallExpression)expression.Body; var interceptMethod = this.GetType().GetMethod("InvokeBaseIntercept", BindingFlags.NonPublic | BindingFlags.Instance); expression = Expression.Lambda( Expression.Call( Expression.Constant(this), interceptMethod, Expression.Constant(bi), Expression.Constant(call.Method), Expression.Constant(mockObject), Expression.NewArrayInit(typeof(object), call.Arguments.Select(arg => arg.CastTo<object>()).ToArray()) ), expression.Parameters ); System.Diagnostics.Debug.WriteLine("Rewritten Expression: " + expression); //do the invocation (could result in recursive invocations expression.Compile().InvokePreserveStack(mockObject); return bi.BaseInvocationResult.ReturnValue; }