您当前的位置: 首页 >  c#

光怪陆离的节日

暂无认证

  • 2浏览

    0关注

    1003博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#程序设计Attribute特性的概念和用法

光怪陆离的节日 发布时间:2022-05-19 17:14:57 ,浏览量:2

本文讲解 C#程序设计Attribute特性的概念和用法。

  1. 操作流程 1.1. Attribute概念 1.1.1. 概念 属性(Attribute)是C#程序设计中非常重要的一个技术,应用范围广泛,用法灵活多变。本文就以实例形式分析了C#中属性的应用。 1.1.2. 运用范围 程序集,模块,类型(类,结构,枚举,接口,委托),字段,方法(含构造),方法,参数,方法返回值,属性(property),Attribute 在这里插入图片描述 在这里插入图片描述

1.2. 自定义Attribute 1.2.1. 自定义一个检查字符串长度的Attribute 为了符合“公共语言规范(CLS)”的要求,所有的自定义的Attribute都必须继承System.Attribute。 第一步:自定义一个检查字符串长度的Attribute 在这里插入图片描述

AttributeUsage这个系统提供的一个Attribute,作用来限定自定义的Attribute作用域,这里我们只允许这个Attribute运用在Property上,内建一个带参的构造器,让外部传入要求的最大长度。 AttributeUsage AttributeTargets在C#的类中,有的类加上了[AttributeUsage(AttributeTargets.Property)]这个是起什么作用的呢?AttributeTargets 枚举

在这里插入图片描述 在这里插入图片描述

1.2.2. 创建一个实体类来运行自定义的属性 在这里插入图片描述

定义了两个字符串字段Name和Description, Name要求最大长度为8个,Description要求最大长度为15. 1.2.3. 创建验证的类 public class ValidationModel {

public void Validate(object obj) { var t = obj.GetType();

//由于我们只在Property设置了Attibute,所以先获取Property
var properties = t.GetProperties();
foreach (var property in properties)
{

  //这里只做一个stringlength的验证,这里如果要做很多验证,需要好好设计一下,千万不要用if elseif去链接
  //会非常难于维护,类似这样的开源项目很多,有兴趣可以去看源码。
  if (!property.IsDefined(typeof(StringLengthAttribute), false)) continue;

  var attributes = property.GetCustomAttributes();
  foreach (var attribute in attributes)
  {
    //这里的MaximumLength 最好用常量去做
    var maxinumLength = (int)attribute.GetType().
      GetProperty("MaximumLength").
      GetValue(attribute);

    //获取属性的值
    var propertyValue = property.GetValue(obj) as string;
    if (propertyValue == null)
      throw new Exception("exception info");//这里可以自定义,也可以用具体系统异常类

    if (propertyValue.Length > maxinumLength)
      throw new Exception(string.Format("属性{0}的值{1}的长度超过了{2}", property.Name, propertyValue, maxinumLength));
  }
}

} }

在这里插入图片描述

这里用到了反射,因为Attribute一般都会和反射一起使用,这里验证了字符串长度是否超过所要求的,如果超过了则会抛出异常 private static void Main(string[] args) { var people = new People() { Name = “qweasdzxcasdqweasdzxc”, Description = “description” }; try { new ValidationModel().Validate(people); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } 1.3. 实际效果测试 建立C#控制台应用程序,实例化一个对象,并赋值,如下所示: 在这里插入图片描述

 运行结果如下,可知Description属性超过了15,因此在控制台显示。

在这里插入图片描述

1.4. 调试过程: 首先People实例化对象传到obj 在这里插入图片描述

t获取对象类型 在这里插入图片描述

把Properties属性的类型付给Properties,即People类中的Name和Decription 在这里插入图片描述

然后开始遍历每个属性,首先是第一个Name,赋给Property. 在这里插入图片描述

 然后则是获取Name中定义的特性,如下

在这里插入图片描述 在这里插入图片描述

其中数值8是在People类中的特性定义了 在这里插入图片描述

然后将sttributs集合枚举出来,实际就只有一个,如下所示 在这里插入图片描述

 从属性中获取“MaximumLength”,可知获取的值为8
![在这里插入图片描述](https://img-blog.csdnimg.cn/4f965ea74fc7431a86c6b5f234619637.png)

在这里插入图片描述

接下来的是获取属性的值,即实例化对象的Name属性具体的值,如下所示 在这里插入图片描述

在这里插入图片描述

接下来是判断获取的Name属性的值和特性中定义的最大长度比较,属性值不存在或者超出特性中定义的最大长度,则抛出异常,如下所示: 实际长度是8,因此没有超出。 在这里插入图片描述

同理而言,遍历第二个属性“Decription”,最终的到的比较如下所示 在这里插入图片描述

实例化对象的第二个属性的值超出最大长度,因此会在控制台打印信息,如下所示:

在这里插入图片描述

至此,C#中的Attribute的具体使用测试完成。

关注
打赏
1665731445
查看更多评论
立即登录/注册

微信扫码登录

0.0426s