如何在。net中利用属性对数据进行校验

  介绍

如何在。net中利用属性对数据进行校验?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<强>前言

属性(特性)的概念不在此赘述了,相信有点。net基础的开发人员都明白,用过属性的人也不在少数,毕竟很多框架都提供自定义的属性,类似于Newtonsoft.JSON中JsonProperty, JsonIgnore等

<>强自定义特性

。网框架允许创建自定义特性,用于存储声明性的信息,且可在运行时被检索。该信息根据设计标准和应用程序需要,可与任何目标元素相关。

创建并使用自定义特性包含四个步骤:

<李>

声明自定义特性

<李>

构建自定义特性

<李>

在目标程序元素上应用自定义特性

<李>

通过反射访问特性

<>强声明自定义特性

一个新的自定义特性必须派生自系统。属性类,例如:

public  class  FieldDescriptionAttribute :属性   {   public 才能string  Description {,得到;private ;,}      public 才能FieldDescriptionAttribute (string 描述)   {才能   ,,,Description =,描述;   ,,}   } public  class  UserEntity   {   [FieldDescription才能(“用户名称“))   public 才能string  Name {组,得到,,,}   }

该如何拿到我们标注的信息呢?这时候需要使用反射获取

,,, var  type =, typeof (UserEntity);   ,,var  properties =, type.GetProperties ();   ,,foreach  (var  item 拷贝属性)   ,,{   ,,,,如果(item.IsDefined (typeof (FieldDescriptionAttribute),,真的))   ,,,,{   ,,,,,,var  attribute =, item.GetCustomAttribute (typeof (FieldDescriptionAttribute)), as  FieldDescriptionAttribute;   ,,,,,,Console.WriteLine (attribute.Description);   ,,,,}   以前,,}

执行结果如下:

如何在。净中利用属性对数据进行校验

从执行结果上看,我们拿到了我们想要的数据,那么这个特性在实际使用过程中,到底有什么用途呢?

<强>属性特性妙用

在实际开发过程中,我们的系统总会提供各种各样的对外接口,其中参数的校验是必不可少的一个环节。然而没有特性时,校验的代码是这样的:

, public  class  UserEntity   ,{   ,,///,& lt; summary>   ,,///,姓名   ,,///,& lt;/summary>   ,,[FieldDescription(“用户名称“))   ,,public  string  Name {组,得到,,,}      ,,///,& lt; summary>///,,,年龄   ,,///,& lt;/summary>   ,,public  int  Age {组,得到,,,}      ,,///,& lt; summary>   ,,///,地址   ,,///,& lt;/summary>   ,,public  string  Address {组,得到,,,}   以前,}
,,, UserEntity  user =, new  UserEntity ();
  
  ,,if  (string.IsNullOrWhiteSpace (user.Name))
  ,,{
  ,,,,throw  new 例外(“姓名不能为空“);
  ,,}
  ,,if  (user.Age  & lt;=, 0)
  ,,{
  ,,,,throw  new 例外(“年龄不合法“);
  ,,}
  ,,if  (string.IsNullOrWhiteSpace (user.Address))
  ,,{
  ,,,,throw  new 例外(“地址不能为空“);
  以前,,} 

字段多了之后这种代码就看着非常繁琐,并且看上去不直观。对于这种繁琐又恶心的代码,有什么方法可以优化呢?
使用特性后的验证写法如下:

首先定义一个基础的校验属性,提供基础的校验方法

,, public  abstract  class  AbstractCustomAttribute :属性   {才能   ,,,///,& lt; summary>   ,,,///,校验后的错误信息   ,,,///,& lt;/summary>   ,,,public  string  ErrorMessage {组,得到,,,}      ,,,///,& lt; summary>   ,,,///,数据校验   ,,,///,& lt;/summary>   ,,,///,& lt; param  name=皏alue"祝辞& lt;/param>   ,,,public  abstract  void 验证(object 价值);   以前,,}

然后可以定义常用的一些对应的校验属性,例如RequiredAttribute, StringLengthAttribute

///, & lt; summary>   ,,,///,非空校验   ,,,///,& lt;/summary>   ,,,(AttributeUsage (AttributeTargets.Property)]   ,,,public  class  RequiredAttribute : AbstractCustomAttribute   ,,,{   ,,,,,public  override  void 验证(object 价值)   ,,,,,{   ,,,,,,,if  (value ==, null  | |, string.IsNullOrWhiteSpace (value.ToString ()))   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

如何在。net中利用属性对数据进行校验