예제 #1
0
        public Castle.DynamicProxy.IInterceptor[] SelectInterceptors(Type type, MethodInfo method, Castle.DynamicProxy.IInterceptor[] interceptors)
        {
            //var attributes = method.GetCustomAttributes<Intercept2Attribute>();
            //if (type.IsClass)
            //    attributes = type.GetMethod(method.Name).GetCustomAttributes<Intercept2Attribute>();

            var allCustomAttributes = method.GetCustomAttributes()
                                            .Concat(type.GetMethod(method.Name).GetCustomAttributes())
                                            .Concat(type.GetCustomAttributes())
                                            .ToList();

            var allInterceptorAttributes = allCustomAttributes.Where(attr => attr.GetType() == typeof(Intercept2Attribute));

            var allInterceptors = allCustomAttributes
                                                .Where(attr => typeof(BaseInterceptAttribute).IsAssignableFrom(attr.GetType()));

            List<Type> addedInterceptorTypes = interceptors.Select(i => i.GetType()).ToList();
            List<Castle.DynamicProxy.IInterceptor> outInterceptors = interceptors.ToList();

            foreach (var attribute in allInterceptorAttributes)
            {
                var interceptorTypes = (attribute as Intercept2Attribute).InterceptorTypes;
                foreach (var interceptorType in interceptorTypes)
                {
                    var interceptor = interceptors.FirstOrDefault(i => i.GetType() == interceptorType);
                    if (interceptor == null)
                        interceptor = Activator.CreateInstance(interceptorType) as IInterceptor;
                    if (!addedInterceptorTypes.Contains(interceptorType))
                    {
                        addedInterceptorTypes.Add(interceptorType);
                        outInterceptors.Add(interceptor);
                    }
                }
            }

            foreach (var interceptor in allInterceptors)
            {
                var interceptAttribute = (interceptor as BaseInterceptAttribute);

                var interceptorType = interceptAttribute.InterceptType;

                if (interceptAttribute.Enabled)
                {
                    var addInterceptor = interceptors.FirstOrDefault(i => i.GetType() == interceptorType);
                    if (addInterceptor == null)
                        addInterceptor = Activator.CreateInstance(interceptorType) as IInterceptor;
                    if (!addedInterceptorTypes.Contains(interceptorType))
                    {
                        addedInterceptorTypes.Add(interceptorType);
                        outInterceptors.Add(addInterceptor);
                    }
                }
            }

            return outInterceptors.ToArray();
        }