首页 > 代码库 > C# 实体类非空验证

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;
            }
        }

 

C# 实体类非空验证