Attribute(特性)的概念不在此赘述了,相信有点.NET基础的开发人员都明白,用过Attribute的人也不在少数,毕竟不少框架都提供自定义的属性,相似于Newtonsoft.JSON中JsonProperty、JsonIgnore等html
.NET 框架容许建立自定义特性,用于存储声明性的信息,且可在运行时被检索。该信息根据设计标准和应用程序须要,可与任何目标元素相关。数组
建立并使用自定义特性包含四个步骤:框架
一个新的自定义特性必须派生自System.Attribute类,例如:ide
public class FieldDescriptionAttribute : Attribute { public string Description { get; private set; } public FieldDescriptionAttribute(string description) { Description = description; } }
public class UserEntity { [FieldDescription("用户名称")] public string Name { get; set; } }
该如何拿到咱们标注的信息呢?这时候须要使用反射获取优化
var type = typeof(UserEntity); var properties = type.GetProperties(); foreach (var item in properties) { if(item.IsDefined(typeof(FieldDescriptionAttribute), true)) { var attribute = item.GetCustomAttribute(typeof(FieldDescriptionAttribute)) as FieldDescriptionAttribute; Console.WriteLine(attribute.Description); } }
执行结果以下:
ui
从执行结果上看,咱们拿到了咱们想要的数据,那么这个特性在实际使用过程当中,到底有什么用途呢?this
在实际开发过程当中,咱们的系统总会提供各类各样的对外接口,其中参数的校验是必不可少的一个环节。然而没有特性时,校验的代码是这样的:设计
public class UserEntity { /// <summary> /// 姓名 /// </summary> [FieldDescription("用户名称")] public string Name { get; set; } /// <summary> /// 年龄 /// </summary> public int Age { get; set; } /// <summary> /// 地址 /// </summary> public string Address { get; set; } }
UserEntity user = new UserEntity(); if (string.IsNullOrWhiteSpace(user.Name)) { throw new Exception("姓名不能为空"); } if (user.Age <= 0) { throw new Exception("年龄不合法"); } if (string.IsNullOrWhiteSpace(user.Address)) { throw new Exception("地址不能为空"); }
字段多了以后这种代码就看着很是繁琐,而且看上去不直观。对于这种繁琐又恶心的代码,有什么方法能够优化呢?
使用特性后的验证写法以下:code
首先定义一个基础的校验属性,提供基础的校验方法htm
public abstract class AbstractCustomAttribute : Attribute { /// <summary> /// 校验后的错误信息 /// </summary> public string ErrorMessage { get; set; } /// <summary> /// 数据校验 /// </summary> /// <param name="value"></param> public abstract void Validate(object value); }
而后能够定义经常使用的一些对应的校验Attribute,例如RequiredAttribute、StringLengthAttribute
/// <summary> /// 非空校验 /// </summary> [AttributeUsage(AttributeTargets.Property)] public class RequiredAttribute : AbstractCustomAttribute { public override void Validate(object value) { if (value == null || string.IsNullOrWhiteSpace(value.ToString())) { throw new Exception(string.IsNullOrWhiteSpace(ErrorMessage) ? "字段不能为空" : ErrorMessage); } } } /// <summary> /// 自定义验证,验证字符长度 /// </summary> [AttributeUsage(AttributeTargets.Property)] public class StringLengthAttribute : AbstractCustomAttribute { private int _maxLength; private int _minLength; public StringLengthAttribute(int minLength, int maxLength) { this._maxLength = maxLength; this._minLength = minLength; } public override void Validate(object value) { if (value != null && value.ToString().Length >= _minLength && value.ToString().Length <= _maxLength) { return; } throw new Exception(string.IsNullOrWhiteSpace(ErrorMessage) ? $"字段长度必须在{_minLength}与{_maxLength}之间" : ErrorMessage); } }
添加一个用于校验的ValidateExtensions
public static class ValidateExtensions { /// <summary> /// 校验 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static void Validate<T>(this T entity) where T : class { Type type = entity.GetType(); foreach (var item in type.GetProperties()) { //须要对Property的字段类型作区分处理针对Object List 数组须要作区分处理 if (item.PropertyType.IsPrimitive || item.PropertyType.IsEnum || item.PropertyType.IsValueType || item.PropertyType == typeof(string)) { //若是是基元类型、枚举类型、值类型或者字符串 直接进行校验 CheckProperty(entity, item); } else { //若是是引用类型 var value = item.GetValue(entity, null); CheckProperty(entity, item); if (value != null) { if ((item.PropertyType.IsGenericType && Array.Exists(item.PropertyType.GetInterfaces(), t => t.GetGenericTypeDefinition() == typeof(IList<>))) || item.PropertyType.IsArray) { //判断IEnumerable var enumeratorMI = item.PropertyType.GetMethod("GetEnumerator"); var enumerator = enumeratorMI.Invoke(value, null); var moveNextMI = enumerator.GetType().GetMethod("MoveNext"); var currentMI = enumerator.GetType().GetProperty("Current"); int index = 0; while (Convert.ToBoolean(moveNextMI.Invoke(enumerator, null))) { var currentElement = currentMI.GetValue(enumerator, null); if (currentElement != null) { currentElement.Validate(); } index++; } } else { value.Validate(); } } } } } private static void CheckProperty(object entity, PropertyInfo property) { if (property.IsDefined(typeof(AbstractCustomAttribute), true))//此处是重点 { //此处是重点 foreach (AbstractCustomAttribute attribute in property.GetCustomAttributes(typeof(AbstractCustomAttribute), true)) { if (attribute == null) { throw new Exception("AbstractCustomAttribute not instantiate"); } attribute.Validate(property.GetValue(entity, null)); } } } }
新的实体类
public class UserEntity { /// <summary> /// 姓名 /// </summary> [Required] public string Name { get; set; } /// <summary> /// 年龄 /// </summary> public int Age { get; set; } /// <summary> /// 地址 /// </summary> [Required] public string Address { get; set; } [StringLength(11, 11)] public string PhoneNum { get; set; } }
调用方式
UserEntity user = new UserEntity(); user.Validate();
上面的校验逻辑写的比较复杂,主要是考虑到对象中包含复杂对象的状况,若是都是简单对象,能够不用考虑,只需针对单个属性作字段校验
现有的方式是在校验不经过的时候抛出异常,此处你们也能够自定义异常来表示校验的问题,也能够返回自定义的校验结果实体来记录当前是哪一个字段出的问题,留待你们本身实现
若是您有更好的建议和想法欢迎提出,共同进步
以上代码均为原创分享,若你们认为有不妥的地方,烦请留言指出,在下感激涕零