Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="invocation">方法调用信息</param>
        public void Intercept(IInvocation invocation)
        {
            //If method call is for generic type (T)...
            if (DynamicApiControllerActionHelper.IsMethodOfType(invocation.Method, typeof(T)))
            {
                //Call real object's method
                try
                {
                    invocation.ReturnValue = invocation.Method.Invoke(_proxiedObject, invocation.Arguments);
                }
                catch (TargetInvocationException targetInvocation)
                {
                    if (targetInvocation.InnerException != null)
                    {
                        targetInvocation.InnerException.ReThrow();
                    }

                    throw;
                }
            }
            else
            {
                //Call api controller's methods as usual.
                invocation.Proceed();
            }
        }
        /// <summary>
        /// Intercepts method calls of dynamic api controller
        /// </summary>
        /// <param name="invocation">Method invocation information</param>
        public void Intercept(IInvocation invocation)
        {
            //If method call is for generic type (T)...
            if (DynamicApiControllerActionHelper.IsMethodOfType(invocation.Method, typeof(T)))
            {
                //Call real object's method
                try
                {
                    // *King
                    //var dm = new DynamicMethod("test", invocation.Method.ReturnType, invocation.Method.GetParameters().Select(p=>p.ParameterType).ToAr-ray());
                    //var il = dm.GetILGenerator();
                    //il.EmitCall(OpCodes.Call, invocation.Method, null);
                    //dm.Invoke(_proxiedObject, invocation.Arguments);
                    invocation.ReturnValue = invocation.Method.Invoke(_proxiedObject, invocation.Arguments);
                }
                catch (TargetInvocationException targetInvocation)
                {
                    if (targetInvocation.InnerException != null)
                    {
                        targetInvocation.InnerException.ReThrow();
                    }

                    throw;
                }
            }
            else
            {
                //Call api controller's methods as usual.
                invocation.Proceed();
            }
        }
        public void Should_Find_Right_Methods()
        {
            var methods = DynamicApiControllerActionHelper.GetMethodsOfType(typeof(IMyApplicationService));

            methods.Count.ShouldBe(4);
            foreach (var method in methods)
            {
                DynamicApiControllerActionHelper.IsMethodOfType(method, typeof(IMyApplicationService)).ShouldBe(true);
            }
        }
 public void Intercept(IInvocation invocation)
 {
     if (DynamicApiControllerActionHelper.IsMethodOfType(invocation.Method, typeof(T)))
     {
         try
         {
             invocation.ReturnValue = invocation.Method.Invoke(_proxiedObject, invocation.Arguments);
         }catch (TargetInvocationException targetInvocation)
         {
             if (targetInvocation.InnerException != null)
             {
                 ExceptionDispatchInfo.Capture(targetInvocation.InnerException).Throw();
             }
             throw;
         }
     }
     else
     {
         // 正常调用api controller的方法
         invocation.Proceed();
     }
 }