Example #1
0
        /// <summary>Select interceptors which must be applied to the method.</summary>
        /// <param name="type">The type.</param>
        /// <param name="method">The method.</param>
        /// <param name="interceptors">The interceptors.</param>
        /// <returns>The interceptors after filtering.</returns>
        public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
        {
            if (method.IsGetter())
            {
                var propertyInfo = type.GetProperty(method);
                if (propertyInfo.IsPropertyWithSelectorAttribute())
                {
                    return typeof(BaseHtmlElement).IsAssignableFrom(method.ReturnType) 
                        ? interceptors.Where(x => x is PropertyInterceptor).ToArray() 
                        : interceptors.Where(x => x is CollectionPropertyInterceptor).ToArray();
                }
            }

            if (method.IsSetter())
            {
                var propertyInfo = type.GetProperty(method);
                if (propertyInfo.IsPropertyWithSelectorAttribute())
                {
                    return interceptors.Where(x => x is InvalidWriteOperationInterceptor).ToArray();
                }
            }

            return interceptors.Where(x => !(x is PropertyInterceptor) 
                && !(x is CollectionPropertyInterceptor)
                && !(x is InvalidWriteOperationInterceptor)).ToArray();
        }
        /// <summary>Invoked by the generation process to know if
        /// the specified member should be intercepted.</summary>
        /// <param name="type">The type.</param>
        /// <param name="methodInfo">The method Info.</param>
        /// <returns>The true if should otherwise false.</returns>
        public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo) 
        {
            this.SetProxiedTypeIfNeed(type);
            if (!methodInfo.IsGetter() && !methodInfo.IsSetter())
            {
                return false;
            }

            var propertyInfo = type.GetProperty(methodInfo);
            this.AppedProcessedProperty(propertyInfo);
            if (!propertyInfo.IsPropertyWithSelectorAttribute())
            {
                return false;
            }

            if (!propertyInfo.IsAutoProperty())
            {
                this.AppendError("'{0}' is not an auto property. Selector attributes can be used only for auto properties.".F(propertyInfo.Name));

                return false;
            }

            var propertyType = propertyInfo.PropertyType;
            var isValidCollectionType = IsValidCollectionType(propertyType);
            if (isValidCollectionType && propertyType.GetGenericArguments().Single().IsAbstract)
            {
                var errorMessage = "'{0}' property has invalid type. Generic type argument can not be abstract. For Elements use ReadOnlyCollection<ButtonElement> instead of ReadOnlyCollection<BaseHtmlElement>. For Controls use non abstract Control type as generic argument for collection."
                    .F(propertyInfo.Name);
                this.AppendError(errorMessage);
                return false;
            }

            if (!isValidCollectionType && !typeof(BaseHtmlElement).IsAssignableFrom(propertyType))
            {
                var errorMessage = "'{0}' property has invalid type. Selector attributes can be used only for properties: \r\n - with type derived from BaseHtmlElement or Contro<T>\r\n - assignable from ReadOnlyCollection<T> where T : BaseHtmlElement or Control<T>"
                    .F(propertyInfo.Name);
                this.AppendError(errorMessage);
                return false;
            }

            if (!isValidCollectionType && propertyType.IsAbstract)
            {
                this.AppendError("'{0}' property has invalid type. Selector attributes can not be used for abstract types.".F(propertyInfo.Name));
                return false;
            }

            return true;
        }