0

有人可以告诉我如何检查参数“ ”是否具有从对象value调用的属性,如果确实如此,则将 value = FileName 作为其值 - 检查下面代码中的评论FileNameHttpPostedFileBase

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        // get a reference to the property this validation depends upon
        var containerType = validationContext.ObjectInstance.GetType();
        var field = containerType.GetProperty(DependentProperty);

        if (field != null)
        {
            // get the value of the dependent property
            var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
            // trim spaces of dependent value
            if (dependentValue != null && dependentValue is string)
            {
                dependentValue = (dependentValue as string).Trim();

                if (!AllowEmptyStrings && (dependentValue as string).Length == 0)
                {
                    dependentValue = null;
                }
            }

            /* test value parameter here 
            if its has a property called FileName then 
            make value = FileName ;*/

            // compare the value against the target value
            if ((dependentValue == null && TargetValue == null) ||
                (dependentValue != null && (TargetValue.Equals("*") || dependentValue.Equals(TargetValue))))
            {
                // match => means we should try validating this field
                if (!_innerAttribute.IsValid(value))
                    // validation failed - return an error
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName });
            }
        }

        return ValidationResult.Success;
    }
4

1 回答 1

0

但是,我不确定我是否正确理解了您的问题:

var valueType = value.GetType()
var fileNameProperty = valueType.GetProperty("FileName");
if(fileNameProperty != null)
    value = fileNameProperty.GetValue(value);
于 2013-09-07T09:19:53.840 回答