/// <summary>
        /// Gets the <see cref="PropertyValidator"/> instances for the specified <paramref name="type"/>.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> that the validators should be retrieved for.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> instance, containing <see cref="IPropertyValidator"/> objects.</returns>
        public IEnumerable<IPropertyValidator> GetValidators(Type type)
        {
            var typeDescriptor =
                new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);

            var propertyDescriptors =
                typeDescriptor.GetProperties();

            var results =
                new List<IPropertyValidator>();

            foreach (PropertyDescriptor descriptor in propertyDescriptors)
            {
                var attributes =
                    descriptor.Attributes.OfType<ValidationAttribute>();

                var validator =
                    new PropertyValidator
                    {
                        AttributeAdaptors = this.GetAttributeAdaptors(attributes),
                        Descriptor = descriptor
                    };

                results.Add(validator);
            }

            return results;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Validates the specified validation context.
 /// </summary>
 /// <param name="validationContext">The validation context.</param>
 /// <param name="metadata">The metadata.</param>
 /// <returns></returns>
 public IEnumerable<ValidationResult> Validate(ValidationContext validationContext, ModelMetadata metadata) {
     if (!metadata.IsRequired || VirtualPath != null) yield break;
     var customTypeDescriptor = new AssociatedMetadataTypeTypeDescriptionProvider(metadata.ContainerType).GetTypeDescriptor(metadata.ContainerType);
     if (customTypeDescriptor == null) yield break;
     var descriptor = customTypeDescriptor.GetProperties().Find(metadata.PropertyName, true);
     var req = (new List<Attribute>(descriptor.Attributes.OfType<Attribute>())).OfType<RequiredAttribute>().FirstOrDefault();
     if (req != null) yield return new ValidationResult(req.FormatErrorMessage(metadata.DisplayName));
 }
Ejemplo n.º 3
0
        private object BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            //针对目标类型创建一个空Model对象
            Type modelType = bindingContext.ModelType;
            object model = this.CreateModel(controllerContext, bindingContext, modelType);
            bindingContext.ModelMetadata.Model = model;

            //针对每个描述属性的PropertyDescriptor对象调用BindProperty方法对相应属性赋值
            ICustomTypeDescriptor modelTypeDescriptor = new AssociatedMetadataTypeTypeDescriptionProvider(modelType).GetTypeDescriptor(modelType);
            PropertyDescriptorCollection propertyDescriptors = modelTypeDescriptor.GetProperties();
            foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors)
            {
                this.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }
            return model;
        }
        private static List<IDataAnnotationsValidatorAdapter> GetAdapters(Type type)
        {
            var typeDescriptor =
                new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);

            var adapters =
                GetAdapters(null, type, typeDescriptor.GetAttributes().OfType<ValidationAttribute>());

            var propertyDescriptors =
                typeDescriptor.GetProperties();

            foreach (PropertyDescriptor property in propertyDescriptors)
            {
                adapters.AddRange(GetAdapters(property, property.PropertyType, property.Attributes.OfType<ValidationAttribute>()));
            }

            return adapters;
        }
Ejemplo n.º 5
0
        public ParameterHandler(ParameterInfo parameterInfo)
        {
            if (IsSimpleType(parameterInfo.ParameterType) || IsEnumerable(parameterInfo.ParameterType))
            {
                GetRouteValues =
                    new ReadOnlyCollection<RouteValueHandler>(new List<RouteValueHandler>()
                    {
                        new RouteValueHandler(parameterInfo.Name, v => v)
                    });
            }
            else
            {
                var type = parameterInfo.ParameterType;
                var typeDesc = new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);
                var propDescs = typeDesc.GetProperties();
                GetRouteValues = propDescs.OfType<PropertyDescriptor>()
                    .Select(desc => new RouteValueHandler(desc.Name, desc.GetValue)).ToList();
            }

            IsFromUri = true;
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Validates the specified validation context.
 /// </summary>
 /// <param name="validationContext">The validation context.</param>
 /// <param name="metadata">The metadata.</param>
 /// <returns></returns>
 public IEnumerable<ValidationResult> Validate(ValidationContext validationContext, ModelMetadata metadata)
 {
     if (!IsValidInput()) {
         yield return new ValidationResult(string.Format("Value of the {0} field couldn´t be recognized as a valid page ", metadata.DisplayName));
     } else if (metadata.IsRequired && !IsValidPage()) {
         var customTypeDescriptor = new AssociatedMetadataTypeTypeDescriptionProvider(metadata.ContainerType).GetTypeDescriptor(metadata.ContainerType);
         if (customTypeDescriptor != null) {
             var descriptor = customTypeDescriptor.GetProperties().Find(metadata.PropertyName, true);
             var req = (new List<Attribute>(descriptor.Attributes.OfType<Attribute>())).OfType<RequiredAttribute>().FirstOrDefault();
             if (req != null) yield return new ValidationResult(req.FormatErrorMessage(metadata.DisplayName));
         }
     }
 }