public void Intercept(IInvocation invocation) { var methondInterceptorAttributes = _methodFilters.GetOrAdd($"{invocation.TargetType.FullName}#{invocation.MethodInvocationTarget.Name}", key => { var methondAttributes = invocation.MethodInvocationTarget.GetCustomAttributes(true) .Where(i => typeof(AbstractInterceptorAttribute).IsAssignableFrom(i.GetType())) .Cast <AbstractInterceptorAttribute>().ToList(); var classInterceptorAttributes = invocation.TargetType.GetCustomAttributes(true) .Where(i => typeof(AbstractInterceptorAttribute).IsAssignableFrom(i.GetType())) .Cast <AbstractInterceptorAttribute>(); methondAttributes.AddRange(classInterceptorAttributes); return(methondAttributes); }); PropertyInject.PropertiesInject(_serviceProvider, methondInterceptorAttributes); if (methondInterceptorAttributes.Any()) { AspectPiplineBuilder aspectPipline = new AspectPiplineBuilder(); foreach (var item in methondInterceptorAttributes) { aspectPipline.Use(item.InvokeAsync); } AspectContext aspectContext = new DefaultAspectContext(invocation); var aspectDelegate = aspectPipline.Build(context => { invocation.Proceed(); aspectContext.ReturnValue = invocation.ReturnValue; return(Task.CompletedTask); }); aspectDelegate.Invoke(aspectContext).GetAwaiter().GetResult(); } }
public void ServiceProvider_Null_Test() { var type = typeof(object); var method = type.GetTypeInfo().GetMethod("Equals", new Type[] { typeof(object) }); var parameter = method.GetParameters().First(); var context = new DefaultAspectContext(null, new TargetDescriptor(new object(), method, type, method, type), new ProxyDescriptor(new object(), method, type), new ParameterCollection(EmptyArray <object> .Value, EmptyArray <ParameterInfo> .Value), new ReturnParameterDescriptor(null, parameter)); Assert.NotNull(context); var exception = Assert.Throws <NotImplementedException>(() => context.ServiceProvider); Assert.Equal(exception.Message, "The current context does not support IServiceProvider."); }