Example #1
0
        private static bool DispatchInvocation(Invocation invocation)
        {
            DebugView.TraceEvent(IndentLevel.Dispatch, () => String.Format("Intercepted profiler call: {0}", invocation.InputToString()));
            DebugView.PrintStackTrace();

            var mockMixin = invocation.MockMixin;
            var repo      = mockMixin != null ? mockMixin.Repository : MockingContext.ResolveRepository(UnresolvedContextBehavior.CreateNewContextual);

            if (repo == null)
            {
                repo = TryFindGlobalInterceptor(invocation.Method);
            }
            if (repo == null)
            {
                return(false);
            }

            lock (repo)
            {
                repo.DispatchInvocation(invocation);
            }

            if (invocation.CallOriginal)
            {
                DebugView.TraceEvent(IndentLevel.DispatchResult, () => "Calling original implementation");
            }
            else if (invocation.IsReturnValueSet)
            {
                DebugView.TraceEvent(IndentLevel.DispatchResult, () => String.Format("Returning value '{0}'", invocation.ReturnValue));
            }

            return(true);
        }
        public void Intercept(IInvocation invocation)
        {
            if (ProfilerInterceptor.ReentrancyCounter > 0)
            {
                CallOriginal(invocation, false);
                return;
            }

            bool callOriginal = false;

            ProfilerInterceptor.GuardInternal(() =>
            {
                var mockInvocation = new Invocation(invocation.Proxy, invocation.GetConcreteMethod(), invocation.Arguments);

                DebugView.TraceEvent(IndentLevel.Dispatch, () => String.Format("Intercepted DP call: {0}", mockInvocation.InputToString()));
                DebugView.PrintStackTrace();

                var mock = mockInvocation.MockMixin;
                var repo = mock != null ? mock.Repository : this.constructionRepo;

                lock (repo)
                {
                    repo.DispatchInvocation(mockInvocation);
                }

                invocation.ReturnValue = mockInvocation.ReturnValue;
                callOriginal           = mockInvocation.CallOriginal;

                if (callOriginal)
                {
                    DebugView.TraceEvent(IndentLevel.DispatchResult, () => "Calling original implementation");
                }
                else if (mockInvocation.IsReturnValueSet)
                {
                    DebugView.TraceEvent(IndentLevel.DispatchResult, () => String.Format("Returning value '{0}'", invocation.ReturnValue));
                }
            });

            if (callOriginal)
            {
                CallOriginal(invocation, true);
            }
        }