public void Intercept(IInvocation invocation) { if (!_aspectValidator.Validate(invocation.Method, true) && !_aspectValidator.Validate(invocation.MethodInvocationTarget, false)) { invocation.Proceed(); return; } if (invocation.Proxy == null) { return; } var proxyTypeInfo = invocation.Proxy.GetType().GetTypeInfo(); var builderFactory = new WindsorAspectBuilderFactory(_aspectBuilderFactory, ctx => { invocation.Proceed(); ctx.AwaitIfAsync(invocation.ReturnValue); ctx.ReturnValue = invocation.ReturnValue; return(Task.FromResult(0)); }); var proxyMethod = proxyTypeInfo.GetMethodBySignature(invocation.Method); var activator = new AspectActivatorFactory(_aspectContextFactory, builderFactory).Create(); var activatorContext = new AspectActivatorContext(invocation.Method, invocation.MethodInvocationTarget, proxyMethod, invocation.InvocationTarget, invocation.Proxy, invocation.Arguments); var reflector = InterceptUtils.GetInvokeReflector(invocation.Method); invocation.ReturnValue = reflector.Invoke(activator, activatorContext); }
private static bool CanDecorate(ServiceRegistration registration, IAspectValidator aspectValidator) { var serviceType = registration.ServiceType.GetTypeInfo(); var implType = registration.GetImplType().GetTypeInfo(); if (implType.IsProxy() || !implType.CanInherited()) { return(false); } if (_excepts.Any(x => implType.Name.Matches(x)) || implType.Namespace != null && _excepts.Any(x => implType.Namespace.Matches(x))) { return(false); } if (!serviceType.CanInherited() || serviceType.IsNonAspect()) { return(false); } if (!aspectValidator.Validate(serviceType, true) && !aspectValidator.Validate(implType, false)) { return(false); } return(true); }
internal bool TryValidate(ServiceDefinition definition, out Type implementationType) { implementationType = null; if (definition.ServiceType.GetTypeInfo().IsNonAspect()) { return(false); } implementationType = definition.GetImplementationType(); if (implementationType == null || implementationType == typeof(object)) { return(false); } if (!implementationType.GetTypeInfo().IsClass) { return(false); } if (definition.ServiceType.GetTypeInfo().IsClass) { if (!(definition is TypeServiceDefinition)) { return(false); } if (!implementationType.GetTypeInfo().CanInherited()) { return(false); } } return(_aspectValidator.Validate(definition.ServiceType, true) || _aspectValidator.Validate(implementationType, false)); }
public static bool Validate(this IAspectValidator aspectValidator, Type type, bool isStrictValidation) { if (aspectValidator == null) { throw new ArgumentNullException(nameof(aspectValidator)); } if (type == null) { return(false); } var typeInfo = type.GetTypeInfo(); if (typeInfo.IsNonAspect() || typeInfo.IsProxyType() || typeInfo.IsValueType || typeInfo.IsEnum || !typeInfo.IsVisible()) { return(false); } if (typeInfo.IsClass && !typeInfo.CanInherited()) { return(false); } foreach (var method in typeInfo.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (aspectValidator.Validate(method, isStrictValidation)) { return(true); } } foreach (var interfaceType in typeInfo.GetInterfaces()) { if (aspectValidator.Validate(interfaceType, isStrictValidation)) { return(true); } } var baseType = typeInfo.BaseType; if (baseType == null || baseType == typeof(object)) { return(false); } return(aspectValidator.Validate(baseType, isStrictValidation)); }
private static bool Validate(ServiceDescriptor descriptor, IAspectValidator aspectValidator) { var implementationType = descriptor.ImplementationType; if (implementationType == null) { return(false); } if (!aspectValidator.Validate(descriptor.ServiceType)) { return(false); } if (implementationType.GetTypeInfo().IsDynamically()) { return(false); } if (!implementationType.GetTypeInfo().CanInherited()) { return(false); } return(true); }
internal bool TryValidate(ServiceDescriptor descriptor, out Type implementationType) { implementationType = null; if (descriptor.ServiceType.GetTypeInfo().IsNonAspect()) { return(false); } if (descriptor.ServiceType.GetTypeInfo().IsGenericTypeDefinition) { return(false); } implementationType = GetImplementationType(descriptor); if (implementationType == null || implementationType == typeof(object)) { return(false); } if (!implementationType.GetTypeInfo().IsClass) { return(false); } if (descriptor.ServiceType.GetTypeInfo().IsClass) { if (descriptor.ImplementationType == null) { return(false); } if (!implementationType.GetTypeInfo().IsVisible()) { return(false); } if (!implementationType.GetTypeInfo().CanInherited()) { return(false); } } return(_aspectValidator.Validate(descriptor.ServiceType, true) || _aspectValidator.Validate(implementationType, false)); }
public ClassProxyTypeGenerator(Type serviceType, Type parentType, Type[] interfaces, IAspectValidator aspectValidator) : base(serviceType, aspectValidator) { if (!parentType.CanInherited()) { throw new InvalidOperationException($"Validate '{parentType}' failed because the type does not satisfy the condition to be inherited."); } ParentType = parentType; Interfaces = interfaces?.Distinct().Where(i => aspectValidator.Validate(i))?.ToArray() ?? Type.EmptyTypes; }
public static bool Validate(this IAspectValidator aspectValidator, PropertyInfo property) { if (aspectValidator == null) { throw new ArgumentNullException(nameof(aspectValidator)); } if (property == null) { throw new ArgumentNullException(nameof(property)); } return((property.CanRead && aspectValidator.Validate(property.GetGetMethod())) || (property.CanWrite && aspectValidator.Validate(property.GetSetMethod()))); }
public static bool Validate(this IAspectValidator aspectValidator, Type typeInfo) { if (aspectValidator == null) { throw new ArgumentNullException(nameof(aspectValidator)); } if (typeInfo == null) { throw new ArgumentNullException(nameof(typeInfo)); } if (typeInfo.IsValueType) { return(false); } return(typeInfo.GetMethods().Any(method => aspectValidator.Validate(method))); }