Beispiel #1
0
        public static void Invoke(this MethodInvocationContext context)
        {
            Console.WriteLine($"real method[{context.ProxyMethod.Name}] invoking...");
            var returnValue = context.MethodBase?.Invoke(context.Target, context.Parameters);

            if (null != returnValue && context.ProxyMethod.ReturnType != typeof(void))
            {
                context.ReturnValue = returnValue;
            }
        }
        public static void InvokeAspectDelegate(MethodInvocationContext context)
        {
            var action = _aspectDelegates.GetOrAdd($"{context.ProxyMethod.DeclaringType}.{context.ProxyMethod}", m =>
            {
                var aspects = new List <AbstractAspect>(8);
                if (context.MethodBase != null)
                {
                    foreach (var aspect in context.MethodBase.GetCustomAttributes <AbstractAspect>())
                    {
                        if (!aspects.Exists(x => x.GetType() == aspect.GetType()))
                        {
                            aspects.Add(aspect);
                        }
                    }
                }

                var methodParameterTypes = context.ProxyMethod.GetParameters().Select(p => p.GetType()).ToArray();
                foreach (var implementedInterface in context.ProxyTarget.GetType().GetImplementedInterfaces())
                {
                    var method = implementedInterface.GetMethod(context.ProxyMethod.Name, methodParameterTypes);
                    if (null != method)
                    {
                        foreach (var aspect in method.GetCustomAttributes <AbstractAspect>())
                        {
                            if (!aspects.Exists(x => x.GetType() == aspect.GetType()))
                            {
                                aspects.Add(aspect);
                            }
                        }
                    }
                }

                var builder = PipelineBuilder.Create <MethodInvocationContext>(x => x.Invoke());
                foreach (var aspect in aspects)
                {
                    builder.Use(aspect.Invoke);
                }
                return(builder.Build());
            });

            action.Invoke(context);

            // check for return value
            if (context.ProxyMethod.ReturnType != typeof(void))
            {
                if (context.ReturnValue == null && context.ProxyMethod.ReturnType.IsValueType)
                {
                    context.ReturnValue = Activator.CreateInstance(context.ProxyMethod.ReturnType);
                }
            }
        }
Beispiel #3
0
 public override void Invoke(MethodInvocationContext methodInvocationContext, Action next)
 {
     Console.WriteLine($"begin invoke method {methodInvocationContext.ProxyMethod.Name} in {GetType().Name}...");
     try
     {
         next();
     }
     catch (Exception e)
     {
         Console.WriteLine($"Invoke {methodInvocationContext.ProxyMethod.DeclaringType?.FullName}.{methodInvocationContext.ProxyMethod.Name} exception");
         Console.WriteLine(e);
     }
     Console.WriteLine($"end invoke method {methodInvocationContext.ProxyMethod.Name} in {GetType().Name}...");
 }
Beispiel #4
0
 public abstract void Invoke(MethodInvocationContext methodInvocationContext, Action next);