GetValidationResult错误消息

时间:2019-07-18 09:52:22

标签: c# validation data-annotations

我想发现一个类的属性并验证该类和方法

public class MyConcreteCommand : IMyCommand
{
    [Required]
    public string MyString { get; set; }
}

public ValidatorResult Validate<T>(T command) where T: IMyCommand
{
    ValidatorResult result = new ValidatorResult();
    foreach (var prop in typeof(T).GetProperties())
    {
        object[] attrs = prop.GetCustomAttributes(true);
        if (attrs == null || attrs.Length == 0)
            continue;

        foreach (Attribute attribute in attrs)
        {
            ValidationResult validationResult = ((ValidationAttribute)attribute).GetValidationResult(prop.GetValue(command), new ValidationContext(command));                        

            string errorMessage = validationResult.
        }
    }
}

问题是错误消息不包含属性名称,例如

  

必填MyString字段

但是是

  

必填MyConcreteCommand字段

0 个答案:

没有答案