public void CallDescriptorConstructionWorksWithNoArguments()
        {
            // --- Act
            var descriptor = new MethodCallDescriptor(this, typeof (MethodDescriptorTest)
                .GetMethod("ConstructionWorksWithNoArguments"), null);

            // --- Assert
            descriptor.ArgumentCount.ShouldEqual(0);
        }
Beispiel #2
0
        /// <summary>
        /// Executes the method call on the target object and wraps it into the execution of the
        /// specified object chain.
        /// </summary>
        /// <param name="target">Target object</param>
        /// <param name="arguments">Method call arguments</param>
        /// <param name="targetMethod"></param>
        /// <param name="aspects">Aspect chain to execute</param>
        /// <param name="methodInfo"></param>
        /// <returns>Result of the method execution</returns>
        public static IMethodResultDescriptor ExecuteWrappedCall <TService>(TService target, List <MethodArgument> arguments,
                                                                            MethodInfo methodInfo, Func <IMethodCallDescriptor, TService, IMethodResultDescriptor> targetMethod,
                                                                            AspectChain aspects)
        {
            // --- Use the aspects of the object, if that supports IMethodAspect
            if (aspects == null)
            {
                var typeAspects = GetAspects(methodInfo, target.GetType());
                if (typeAspects.Count > 0)
                {
                    aspects = new AspectChain(typeAspects);
                }
            }

            var targetAspect = target as IMethodAspect;

            if (targetAspect != null)
            {
                var aspectList = new List <IMethodAspect> {
                    targetAspect
                };
                if (aspects != null)
                {
                    aspectList.AddRange(aspects);
                }
                aspects = new AspectChain(aspectList);
            }

            // --- Obtain the method descriptor
            var args          = new MethodCallDescriptor(target, methodInfo, arguments);
            var onEntryResult = OnEntry(aspects, args);

            if (onEntryResult == null)
            {
                // --- Call original method body
                IMethodResultDescriptor result;
                try
                {
                    result = targetMethod(args, target);
                }
                catch (Exception ex)
                {
                    result = new MethodResultDescriptor(ex);
                }

                // --- Call OnExit
                OnExit(aspects, args, result);
                if (result.Exception == null)
                {
                    onEntryResult = OnSuccess(aspects, args, result);
                }
                else
                {
                    var newEx = OnException(aspects, args, result.Exception);
                    if (newEx == null)
                    {
                        throw new AspectInfrastructureException("OnException returned null.", null);
                    }
                    throw newEx;
                }
            }
            return(onEntryResult);
        }