码迷,mamicode.com
首页 > Windows程序 > 详细

C# 实体类非空验证

时间:2014-10-15 18:46:31      阅读:654      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   log   amp   

 public class BillReceiveModelDTO
    {
        /// <summary>
        /// 
        /// </summary>
        [DataMember]
        [Required(ErrorMessage = "非空")]
        public string orderNo { set; get; }
}

//验证
private static IEnumerable<string> VerifyTelegram(BillReceiveModelDTO telegram)
        {
            foreach (var property in typeof(BillReceiveModelDTO).GetProperties())
            {
                var attr = property.GetAttributes<RequiredAttribute>(false).FirstOrDefault();
                if (attr != null && !attr.IsValid(property.GetValue(telegram, null)))
                    yield return attr.ErrorMessage;
            }
        }

 

C# 实体类非空验证

标签:style   blog   color   ar   for   sp   div   log   amp   

原文地址:http://www.cnblogs.com/zyhblogs/p/4026696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!