public IInterceptorProvider[] GetInterceptorProvidersForMethod(Type targetType, MethodInfo method)
 {
     Guard.ArgumentNotNull(nameof(method), nameof(method));
     return(CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(method)
            .Reverse()
            .ToArray());
 }
 public IInterceptorProvider[] GetInterceptorProvidersForType(Type type)
 {
     Guard.ArgumentNotNull(nameof(type), nameof(type));
     return(CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(type)
            .Reverse()
            .ToArray());
 }
Ejemplo n.º 3
0
        private IEnumerable <IInterceptorProvider> GetInterceptorProvidersForMethod(MethodInfo proxyMethod, object target, out NonInterceptableAttribute nonInterceptableAttribute)
        {
            MethodInfo targetMethod = this.GetTargetMethod(proxyMethod, target);

            if (null == targetMethod)
            {
                nonInterceptableAttribute = null;
                return(new IInterceptorProvider[0]);
            }

            var attribute = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(targetMethod);

            nonInterceptableAttribute = attribute;
            var providers = CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(targetMethod);

            if (null != attribute)
            {
                if (!nonInterceptableAttribute.InterceptorProviderTypes.Any())
                {
                    return(new IInterceptorProvider[0]);
                }
                providers = providers.Where(it => !attribute.InterceptorProviderTypes.Contains(it.GetType()));
            }

            foreach (var provider in providers)
            {
                (provider as IAttributeCollection)?.AddRange(CustomAttributeAccessor.GetCustomAttributes(proxyMethod, true));
                (provider as IAttributeCollection)?.AddRange(CustomAttributeAccessor.GetCustomAttributes(target.GetType(), true));
            }
            return(providers);
        }
        public IInterceptorProvider[] GetInterceptorProvidersForProperty(Type targetType, PropertyInfo property, PropertyMethod propertyMethod)
        {
            Guard.ArgumentNotNull(nameof(property), nameof(property));
            var method = propertyMethod == PropertyMethod.Get ? property.GetMethod : property.SetMethod;

            return(CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(property).ToArray()
                   .Concat(CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(method).ToArray()));
        }
Ejemplo n.º 5
0
 public static bool ShouldProxy(Type serviceType)
 {
     if (typeof(IAspects).IsAssignableFrom(serviceType))
     {
         var attribute = CustomAttributeAccessor.GetCustomAttribute <AspectsAttribute>(serviceType, true);
         return(attribute?.Disable != true);
     }
     return(false);
 }
Ejemplo n.º 6
0
        public IInterceptorProvider[] GetInterceptorProvidersForMethod(Type targetType, MethodInfo method, out ISet <Type> excludedInterceptorProviders)
        {
            Guard.ArgumentNotNull(nameof(method), nameof(method));
            var excludedTypes = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(method)?.InterceptorProviderTypes;

            excludedInterceptorProviders = excludedTypes?.Length > 0
                ? new HashSet <Type>(excludedTypes)
                : new HashSet <Type>();
            return(CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(method).Reverse().ToArray());
        }
        public bool?WillIntercept(Type targetType, PropertyInfo property)
        {
            Guard.ArgumentNotNull(nameof(property), nameof(property));
            var attributes = CustomAttributeAccessor.GetCustomAttributes <NonInterceptableAttribute>(property, true);

            if (attributes.Any(it => it.InterceptorProviderTypes.Length == 0))
            {
                return(false);
            }
            return(null);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the interceptors decorated with the specified type.
        /// </summary>
        /// <param name="typeToIntercept">The type to intercept.</param>
        /// <returns>
        /// The <see cref="T:Dora.DynamicProxy.InterceptorDecoration" /> representing the type members decorated with interceptors.
        /// </returns>
        public InterceptorDecoration GetInterceptors(Type typeToIntercept)
        {
            Guard.ArgumentNotNull(typeToIntercept, nameof(typeToIntercept));
            if (CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(typeToIntercept, true) != null)
            {
                return(InterceptorDecoration.Empty);
            }

            var classProviders  = CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(typeToIntercept, true).ToArray();
            var methodProviders = this.ResolveInterceptorProviders(typeToIntercept);

            return(this.GetInterceptors(typeToIntercept, methodProviders));
        }
Ejemplo n.º 9
0
        private IEnumerable <IInterceptorProvider> GetInterceptorProvidersForClass(object target, out NonInterceptableAttribute nonInterceptableAttribute)
        {
            var attribute = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(target.GetType());

            nonInterceptableAttribute = attribute;
            var providers = target.GetType().GetTypeInfo().GetCustomAttributes().OfType <IInterceptorProvider>();

            if (null != attribute)
            {
                if (!nonInterceptableAttribute.InterceptorProviderTypes.Any())
                {
                    return(new IInterceptorProvider[0]);
                }
                providers = providers.Where(it => !attribute.InterceptorProviderTypes.Contains(it.GetType()));
            }
            foreach (var provider in providers)
            {
                (provider as IAttributeCollection)?.AddRange(CustomAttributeAccessor.GetCustomAttributes(target.GetType(), true));
            }
            return(providers);
        }
Ejemplo n.º 10
0
        internal Dictionary <MethodInfo, InterceptorDelegate> CreateInterceptors(Type typeToProxy)
        {
            Dictionary <MethodInfo, InterceptorDelegate> interceptors = new Dictionary <MethodInfo, InterceptorDelegate>();

            foreach (var method in typeToProxy.GetTypeInfo().GetMethods())
            {
                if (method.DeclaringType != typeToProxy)
                {
                    continue;
                }
                var aspectsAttribute = CustomAttributeAccessor.GetCustomAttribute <AspectsAttribute>(method, true);
                if (aspectsAttribute?.Disable != true)
                {
                    var newBuilder = _chainBuilder.New();
                    foreach (var provider in _interceptBuilder.Providers)
                    {
                        provider.Use(newBuilder);
                    }
                    interceptors[method] = newBuilder.Build();
                }
            }
            return(interceptors);
        }
Ejemplo n.º 11
0
        public IInterceptorProvider[] GetInterceptorProvidersForProperty(Type targetType, PropertyInfo property, PropertyMethod propertyMethod, out ISet <Type> excludedInterceptorProviders)
        {
            Guard.ArgumentNotNull(nameof(property), nameof(property));
            var method = propertyMethod == PropertyMethod.Get ? property.GetMethod : property.SetMethod;

            var list          = new List <Type>();
            var excludedTypes = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(property)?.InterceptorProviderTypes;

            if (excludedTypes?.Length > 0)
            {
                list.AddRange(excludedTypes);
            }
            excludedTypes = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(method)?.InterceptorProviderTypes;
            if (excludedTypes?.Length > 0)
            {
                list.AddRange(excludedTypes);
            }
            excludedInterceptorProviders = new HashSet <Type>(list);

            var providers = CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(property);

            providers.Concat(CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(method));
            return(providers.ToArray());
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Gets the interceptors decorated with the type of target instance.
        /// </summary>
        /// <param name="typeToIntercept">The type to intercept.</param>
        /// <param name="targetType">Type of the target instance.</param>
        /// <returns>
        /// The <see cref="T:Dora.DynamicProxy.InterceptorDecoration" /> representing the type members decorated with interceptors.
        /// </returns>
        public InterceptorDecoration GetInterceptors(Type typeToIntercept, Type targetType)
        {
            Guard.ArgumentNotNull(typeToIntercept, nameof(typeToIntercept));
            Guard.ArgumentNotNull(targetType, nameof(targetType));

            InterceptorDecoration interceptors;

            if (_nonInterceptableTypes.Contains(typeToIntercept))
            {
                return(InterceptorDecoration.Empty);
            }

            if (typeToIntercept == targetType)
            {
                interceptors = GetInterceptors(targetType);
                if (interceptors.MethodBasedInterceptors.Count == 0 && interceptors.PropertyBasedInterceptors.Count == 0)
                {
                    lock (_nonInterceptableTypes)
                    {
                        _nonInterceptableTypes.Add(typeToIntercept);
                    }
                }
                return(interceptors);
            }

            if (!typeToIntercept.IsInterface)
            {
                return(InterceptorDecoration.Empty);
            }

            if (CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(typeToIntercept, true) != null)
            {
                lock (_nonInterceptableTypes)
                {
                    _nonInterceptableTypes.Add(typeToIntercept);
                }
                return(InterceptorDecoration.Empty);
            }

            var mapping      = targetType.GetTypeInfo().GetRuntimeInterfaceMap(typeToIntercept);
            var srcProviders = this.ResolveInterceptorProviders(targetType);
            var providers    = new Dictionary <MethodInfo, IInterceptorProvider[]>();

            foreach (var method in srcProviders.Keys)
            {
                var index = Array.IndexOf(mapping.TargetMethods, method);
                if (index > -1)
                {
                    providers.Add(mapping.InterfaceMethods[index], srcProviders[method]);
                }
            }
            var classProviders = CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(targetType, true).ToArray();

            interceptors = this.GetInterceptors(typeToIntercept, providers);
            if (interceptors.MethodBasedInterceptors.Count == 0 && interceptors.PropertyBasedInterceptors.Count == 0)
            {
                lock (_nonInterceptableTypes)
                {
                    _nonInterceptableTypes.Add(typeToIntercept);
                }
            }
            return(interceptors);
        }
Ejemplo n.º 13
0
        private Dictionary <MethodInfo, IInterceptorProvider[]> ResolveInterceptorProviders(Type type)
        {
            var results = new Dictionary <MethodInfo, IInterceptorProvider[]>();
            var nonInterceptableAttribute = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(type, true);

            if (nonInterceptableAttribute != null && nonInterceptableAttribute.InterceptorProviderTypes.Length == 0)
            {
                return(results);
            }
            var classProviders = CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(type, true).ToArray();

            if (null != nonInterceptableAttribute)
            {
                classProviders = classProviders
                                 .Where(it => !nonInterceptableAttribute.InterceptorProviderTypes.Contains(it.GetType()))
                                 .ToArray();
            }

            foreach (var methodInfo in type.GetTypeInfo().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                if (methodInfo.DeclaringType == typeof(object))
                {
                    continue;
                }

                if (methodInfo.IsSpecialName)
                {
                    continue;
                }

                nonInterceptableAttribute = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(methodInfo, true);
                if (null != nonInterceptableAttribute && nonInterceptableAttribute.InterceptorProviderTypes.Length == 0)
                {
                    continue;
                }
                var providers = CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(methodInfo, true).ToArray();
                var list      = new List <IInterceptorProvider>();
                list.AddRange(providers);
                foreach (var provider in classProviders)
                {
                    if (nonInterceptableAttribute?.InterceptorProviderTypes?.Contains(provider.GetType()) == true)
                    {
                        continue;
                    }

                    if (!provider.AllowMultiple && providers.Any(it => it.GetType() == provider.GetType()))
                    {
                        continue;
                    }
                    list.Add(provider);
                }

                if (list.Count > 0)
                {
                    results.Add(methodInfo, list.ToArray());
                }
            }

            foreach (var property in type.GetTypeInfo().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                nonInterceptableAttribute = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(property, true);
                if (null != nonInterceptableAttribute && nonInterceptableAttribute.InterceptorProviderTypes.Length == 0)
                {
                    continue;
                }
                var propertyProviders = CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(property, true).ToArray();
                var list = new List <IInterceptorProvider>();
                list.AddRange(propertyProviders);
                foreach (var provider in classProviders)
                {
                    if (nonInterceptableAttribute?.InterceptorProviderTypes?.Contains(provider.GetType()) == true)
                    {
                        continue;
                    }

                    if (!provider.AllowMultiple && propertyProviders.Any(it => it.GetType() == provider.GetType()))
                    {
                        continue;
                    }
                    list.Add(provider);
                }

                var methods = new List <MethodInfo>();
                if (property.GetMethod != null)
                {
                    methods.Add(property.GetMethod);
                }
                if (property.SetMethod != null)
                {
                    methods.Add(property.SetMethod);
                }

                foreach (var method in methods)
                {
                    nonInterceptableAttribute = CustomAttributeAccessor.GetCustomAttribute <NonInterceptableAttribute>(method, true);
                    if (nonInterceptableAttribute != null && nonInterceptableAttribute.InterceptorProviderTypes.Length == 0)
                    {
                        continue;
                    }

                    var methodProviders = CustomAttributeAccessor.GetCustomAttributes <IInterceptorProvider>(method, true).ToArray();
                    var list1           = new List <IInterceptorProvider>();
                    list1.AddRange(methodProviders);

                    foreach (var provider in list)
                    {
                        if (nonInterceptableAttribute?.InterceptorProviderTypes?.Contains(provider.GetType()) == true)
                        {
                            continue;
                        }

                        if (!provider.AllowMultiple && methodProviders.Any(it => it.GetType() == provider.GetType()))
                        {
                            continue;
                        }
                        list1.Add(provider);
                    }

                    if (list1.Count > 0)
                    {
                        results[method] = list1.ToArray();
                    }
                }
            }
            return(results);
        }