TryValidateProperty不适用于泛型函数 [英] TryValidateProperty not work with generic function

查看:56
本文介绍了TryValidateProperty不适用于泛型函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已更新:当我执行单元测试项目时,它将返回未处理.此测试结果还包含一个内部异常,而不是"Assert.IsTrue失败."必填字段."结果类似(0次通过,1次失败,1次总计),但是如果我使用F11调试
,我们根本不会遇到任何异常

UPDATED: When i executing Unit Test project and then it will return Was unhandled This test result also contained an inner exception instead of "Assert.IsTrue failed. The Description field is required." Result like (0 Pass, 1 FAIL, 1 Total) but we are not getting any exception at all if I debug with F11

[TestMethod]
[Asynchronous]
[Description("Determines whether the selected or single property is valide using the validation context or validate single properties.")]
        public void ValidateSigleWithDataAnnotation()
        {
            LookupsServices lookupsservices = new LookupsServices();
            Lookups lookups = new Lookups() { Description = "", LookupReference = 2, DisplayOrder = 50};
            lookupsservices.Lookups.Add(lookups);

            //THIS IS NOT WORKING
            string message = ValidateProperties.ValidateSingle(lookups, "Description");
            Assert.IsTrue(message.Equals(""), message);
            //THIS IS WORKING
            var results = new List<ValidationResult>();
            Validator.TryValidateProperty(lookups.Description , new ValidationContext(lookups, null, null) { MemberName = "Description" }, results);
            Assert.IsTrue(results.Count == 0, results[0].ToString());
        }



以下是验证单个属性的通用功能



Following is the Generic function to validate individual property

public static string ValidateSingle<T>(T t, string PeropertyName) where T : class
       {
           string errorMessage = "";
           var ValidationMessages = new List<ValidationResult>();
           bool ValidationResult = Validator.TryValidateProperty(typeof(T).GetProperty(PeropertyName).Name,  new ValidationContext(t, null, null) { MemberName =  PeropertyName} , ValidationMessages);

           if (!ValidationResult) errorMessage += string.Format("\n{0}", ValidationMessages[0]);
           return errorMessage;
       }



以下是其中描述字段ID为必填项的模型



Following is the Model where Description field id Required

public class Lookups
    {
        public Lookups() { }
        [Key]
        public virtual int LookupReference { get; set; }
        [Required]
        public virtual string Description { get; set; }
        public virtual int? DisplayOrder { get; set; }
    }



如果我在没有通用方法的情况下进行验证,则会收到错误必填字段".,但是为什么在使用通用方法时却没有得到相同的错误?



I am getting error "The Description field is required" if I am validating without Generic method, but why am not getting same error using Generic method?

推荐答案

大家好,

我使用下面的代码解决了我的问题.

Hi All,

I resolved my problem using following line of code.

bool ValidationResult = Validator.TryValidateProperty(typeof(T).GetProperty(PeropertyName).Name,  new ValidationContext(t, null, null) { MemberName =  PeropertyName} , ValidationMessages);



在第一种形式中,我要传递属性的名称,即描述".在第二种形式中,我传入属性的值,即".要使第一次调用看起来像第二次调用:



In the first form, I am passing in the name of the property, i.e. "Description". In the second form, I am passing in the value of the property, i.e. "". To make the first call look like the second:

typeof(T).GetProperty(PeropertyName).GetValue(t,null)


解决方案



SOLUTION


bool ValidationResult = Validator.TryValidateProperty(typeof(T).GetProperty(PeropertyName).GetValue(t,null),  new ValidationContext(t, null, null) { MemberName =  PeropertyName} , ValidationMessages);




我想你无法到达
正如您所期望的那样,您断言为 false ,因为您定义了Description =".您的断言总是返回 true ,就像您在上面定义的一样...

问候
Hi,

I think you could not arrived to what
you are asserting as false as you are expecting, because you defined Description = "". Your assertion always return true as what you have defined above...

Regards,


这篇关于TryValidateProperty不适用于泛型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆