C# 实体类非空验证

 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;
            }
        }
原文地址:https://www.cnblogs.com/zyhblogs/p/4026696.html