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 に答える