NET 特性(Attribute)

转自 博客园(Fish)

特性(Attribute):是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。

您可以通过使用特性向程序添加声明性信息。一个声明性标签是通过放置在它所应用的元素前面的方括号([ ])来描述的。

特性(Attribute)用于添加元数据,如编译器指令和注释、描述、方法、类等其他信息。

.Net 框架提供了两种类型的特性:预定义特性和自定义特性。

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

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

  • 声明自定义特性
  • 构建自定义特性
  • 在目标程序元素上应用自定义特性
  • 通过反射访问特性
  1. /// <summary>
  2. /// 别名特性
  3. /// </summary>
  4. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
  5. public class AliasAttribute : Attribute
  6. {
  7. /// <summary>
  8. /// 别名
  9. /// </summary>
  10. public string Name { get; set; }
  11. /// <summary>
  12. /// 别名类型
  13. /// </summary>
  14. public Type Type { get; set; }
  15. /// <summary>
  16. /// 别名名称,类型默认为string
  17. /// </summary>
  18. /// <param name="name"></param>
  19. public AliasAttribute(string name)
  20. {
  21. Name = name;
  22. Type = typeof(string);
  23. }
  24. /// <summary>
  25. /// 别名名称,类型为传入类型
  26. /// </summary>
  27. /// <param name="name"></param>
  28. public AliasAttribute(string name, Type type)
  29. {
  30. Name = name;
  31. Type = type;
  32. }
  33. }

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

  1. public class UserDto
  2. {
  3. /// <summary>
  4. /// 姓名
  5. /// </summary>
  6. [Alias("name", typeof(string))]
  7. public string Name { get; set; }
  8. /// <summary>
  9. /// 电话
  10. /// </summary>
  11. [Alias("phone", typeof(string))]
  12. public string Phone { get; set; }
  13. /// <summary>
  14. /// 备注
  15. /// </summary>
  16. [Alias("remark", typeof(string))]
  17. public string Remark { get; set; }
  18. }

3.通过反射访问特性

  1. // 获取type
  2. var userType = typeof(UserDto);
  3. // 获取类中所有公共属性集合
  4. var PropertyArr = userType.GetProperties();
  5. foreach (var itemProperty in PropertyArr)
  6. {
  7. // 获取属性上存在AliasAttribute的数组
  8. var customAttributesArr = itemProperty.GetCustomAttributes(typeof(AliasAttribute), true);
  9. if (customAttributesArr.Any())
  10. {
  11. // 获取特性
  12. var first = customAttributesArr.FirstOrDefault();
  13. }
  14. else {
  15. // 不存在特性
  16. }
  17. }

NET 特性(Attribute)的更多相关文章

  1. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  2. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  3. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

  4. 区分元素特性attribute和对象属性property

    × 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...

  5. .Net内置特性Attribute介绍

    特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...

  6. 【点滴积累】通过特性(Attribute)为枚举添加更多的信息

    转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...

  7. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

  8. 如何获取类或属性的自定义特性(Attribute)

    如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...

  9. C# 知识特性 Attribute,XMLSerialize,

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用“反射”查询特性,获取特性集合方 ...

  10. c#特性attribute:

    特性是被编译到metadata中,  是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...

随机推荐

  1. java基础(17):包装类、System、Math、Arrays、大数据运算

    1. 基本类型包装类 大家回想下,在第三篇文章中我们学习Java中的基本数据类型时,说Java中有8种基本的数据类型,可是这些数据是基本数据,想对其进行复杂操作,变的很难.怎么办呢? 1.1 基本类型 ...

  2. Java日期时间API系列1-----Jdk7及以前的日期时间类

    先看一个简单的图: 主要的类有: Date类负责时间的表示,在计算机中,时间的表示是一个较大的概念,现有的系统基本都是利用从1970.1.1 00:00:00 到当前时间的毫秒数进行计时,这个时间称为 ...

  3. FCC---Animate Elements Continually Using an Infinite Animation Count---设置animation-iteration-count的次数为无限,让小球一直跳动

    The previous challenges covered how to use some of the animation properties and the @keyframes rule. ...

  4. vue快速复习手册

    1.基本使用 <!DOCTYPE html> <head> <meta charset="UTF-8"> <title>Vue的基本 ...

  5. Dynamics 365本地部署版本配置OAuth 2 Password Grant以调用Web API

    微软动态CRM专家罗勇 ,回复330或者20190504可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! 根据官方建议,不要再使用Dynamics 365 Custome ...

  6. php 的定界符 <<<eof

    PHP是一个Web编程语言,在编程过程中难免会遇到用echo来输出大段的html和javascript脚本的情况,如果用传统的输出方法 ——按字符串输出的话,肯定要有大量的转义符来对字符串中的引号等特 ...

  7. Linux中vim编辑命令

    vim 功能 : 一个强大的文本编辑器   语法格式 :vim [ 选项 ] / 路径 / 文本文件名 命令格式: vi [ 选项 ] [ 文件名 ]   +num 打开某个文件直接跳转到 num 行 ...

  8. 查看sybase IQ的执行计划

    在性能调优工作中,首要的事情是找出性能瓶颈.而针对数据库应用,由于商用数据库对上层应用来说是个黑盒,所以往往需要借助数据库的一些接口或工具来了解数据库的具体行为,并结合相关知识和业务进行调测.    ...

  9. 抓包工具 tcpdump 用法说明

    tcpdump采用命令行方式对接口的数据包进行筛选抓取,其丰富特性表现在灵活的表达式上. 不带任何选项的tcpdump,默认会抓取第一个网络接口,且只有将tcpdump进程终止才会停止抓包. 例如: ...

  10. win7个性化不能换界面:此页面上的一个或多个设置已被系统管理员禁用,关机里的切换用户和锁定为灰色

    win7个性化不能换界面:此页面上的一个或多个设置已被系统管理员禁用,关机里的切换用户和锁定为灰色 找到注册表 cmd-regedit HKEY_CURRENT_USER\Software\Micro ...