예제 #1
0
        private static DefaultMetadataDetails CreateMetadataDetails(ActionExecutionModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var modelMetadataIdentity = ModelMetadataIdentity.ForType(model.GetType());

            var modelAttributes = ModelAttributes.GetAttributesForType(model.GetType());

            var modelMetadataDetails = new DefaultMetadataDetails(modelMetadataIdentity, modelAttributes);

            return(modelMetadataDetails);
        }
예제 #2
0
        private static DefaultMetadataDetails CreateMetadataDetails(ParameterSpecification parameterSpecification, ActionExecutionModel containerModel)
        {
            if (parameterSpecification == null)
            {
                throw new ArgumentNullException(nameof(parameterSpecification));
            }
            if (containerModel == null)
            {
                throw new ArgumentNullException(nameof(containerModel));
            }

#pragma warning disable CS0618 // Type or member is obsolete
            var parameterMetadataIdentity = ModelMetadataIdentity.ForProperty(parameterSpecification.Type, parameterSpecification.Key, containerModel.GetType());
#pragma warning restore CS0618 // Type or member is obsolete

            var parameterAttributes = ModelAttributes.GetAttributesForProperty(containerModel.GetType(), nullPropertyInfo, parameterSpecification.Type);

            var propertyMetadataDetails = new DefaultMetadataDetails(
                parameterMetadataIdentity,
                parameterAttributes)
            {
                PropertyGetter     = thisModel => GetParameterValue((ActionExecutionModel)thisModel, parameterSpecification),
                PropertySetter     = (thisModel, value) => SetParameterValue((ActionExecutionModel)thisModel, parameterSpecification, value),
                ValidationMetadata = new ValidationMetadata
                {
                    IsRequired = parameterSpecification.IsRequired
                },
                DisplayMetadata = new DisplayMetadata()
            };

            var dataTypeAttribute = parameterSpecification.ValidationAttributes.OfType <DataTypeAttribute>().FirstOrDefault();

            if (dataTypeAttribute != null)
            {
                propertyMetadataDetails.DisplayMetadata.DataTypeName = dataTypeAttribute.GetDataTypeName();
            }

            foreach (var attribute in parameterSpecification.ValidationAttributes)
            {
                propertyMetadataDetails.ValidationMetadata.ValidatorMetadata.Add(attribute);
            }

            foreach (var attribute in parameterSpecification.Type.GetCustomAttributes <ValidationAttribute>(true))
            {
                propertyMetadataDetails.ValidationMetadata.ValidatorMetadata.Add(attribute);
            }

            if (propertyMetadataDetails.ValidationMetadata.ValidatorMetadata.Count > 0 || typeof(IValidatableObject).IsAssignableFrom(parameterSpecification.Type))
            {
                propertyMetadataDetails.ValidationMetadata.HasValidators = true;
            }

            return(propertyMetadataDetails);
        }