[AttributeUsage( // AttributeUsage是.net定义的特性的内置特性说明,可以叫特性的元特性。AttributeTargets.All,// 应用到的   程序元素[必选],使用时候如果是assembly或module的可以放
//置在代码中的任何地方AllowMultiple = true, // 同一个程序元素上是否可以使用多次[可选]Inherited = false)] // 是否可以继承,接口/类,如果是属性方法那么重载属性方法中也会影响[可选]// 描述如何使用一个自定义特性 SomethingAttribute//********自定义特性SomethingAttribute**************//public class SomethingAttribute : Attribute{private string name; // 名字private string data; // 日期public string Name{get { return name; }set { name = value; }}public string Data{get { return data; }set { data = value; }}public SomethingAttribute(string name){this.name = name;this.name = name;}}[Something("类外特性", Data = "2018-06-18")]class Testone { }class TestTwo{[Something("类内特性", Data = "2018-06-18")]public string MyId { get; set; }}

应用

/// <summary>/// 反射获得用户类外自定义属性/// </summary>/// <param name="t"></param>private static void PrintSomethingByAttribute(System.Type t){string ss = $"\n类型的 System.Type 对象是:{t}";System.Console.WriteLine(ss);System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  //反射获得用户自定义属性  foreach (System.Attribute attr in attrs){if (attr is SomethingAttribute){SomethingAttribute a = (SomethingAttribute)attr;string str = $"   名称:{a.Name}, 日期: {a.Data}";System.Console.WriteLine(str);}}}/// <summary>/// 反射获得类自定义内属性/// </summary>/// <param name="t"></param>private static void PrintSomethingByInfo(System.Type t){string ss = $"\n类型的 System.Type 对象是:{t}";System.Console.WriteLine(ss);System.Reflection. PropertyInfo [] propertys = t.GetProperties()   ;//返回所有公共属性  if (propertys != null && propertys.Length > 0)  {foreach (System.Reflection.PropertyInfo p in propertys){object[] objAttrs = p.GetCustomAttributes(typeof(SomethingAttribute), true);//获取自定义特性  //GetCustomAttributes(要搜索的特性类型,是否搜索该成员的继承链以查找这些特性)  if (objAttrs != null && objAttrs.Length > 0){foreach (var m in objAttrs){SomethingAttribute attr = m as SomethingAttribute;Console.WriteLine("自定义特性Name:" + p.Name + ", 元数据name:" + attr.Name);}}};}}static void Main(string[] args){//首先,通过typeof+类名也可以获得Type类型的对象。   Type t = typeof(Test);PrintSomethingByAttribute(t);PrintSomethingByInfo(t);t = typeof(TestTwo);PrintSomethingByAttribute(t);PrintSomethingByInfo(t);Console.ReadKey();//其次,利用type对象调用GetCustomAttributes方法,可以获得该类所使用的所有特性;//其中,该方法的参数表示是否搜索该类所继承的父类所使用的特性,false为不搜索;//当我们获取这些特性时,得到的是这些特性所生成的对象,但它们不是特性类的对象;//返回值为一个object类型的数组,保存所有的对象。}

结果

类型的 System.Type 对象是:ConsoleApplication2.Program+Testone
名称:类外特性, 日期: 2018-06-18

类型的 System.Type 对象是:ConsoleApplication2.Program+Testone

类型的 System.Type 对象是:ConsoleApplication2.Program+TestTwo

类型的 System.Type 对象是:ConsoleApplication2.Program+TestTwo
自定义特性Name:MyId, 元数据name:类内特性

C#的自定义属性AttributeUsage用法相关推荐

  1. js第三节-自定义属性、索引值

    js第三节-自定义属性.索引值 第一节陈明吕老师就大致提了一下自定义属性这个概念,自定义属性是我们自己给元素添加的属性,不是元素天生就存在的.自定义属性最常用的是索引值.举个例子来讲一下: 一.自定义 ...

  2. Unity自定义属性(Attribute)示例

    一.自定义属性,使用场景 a.想知道当前的工程内定义过的特定的类 b.想知道当前工程内,类中定义过的特定的成员(变量,方法) 二.自定义属性介绍 a.需要继承自 System.Attrbute. 可以 ...

  3. Android基于Glide的二次封装,借鉴Glide思想二次封装Fresco

    最近封装了个 fresco 的组件库:dfresco,就顺便来讲讲. 背景 fresco 图片库很强大,我们项目中就是使用的 fresco,但有一点就是,不怎么好使用,略麻烦.不同项目中,多多少少都需 ...

  4. .NET WebAPI 用ExceptionFilterAttribute实现错误(异常)日志的记录(log4net做写库操作)...

    好吧,还是那个社区APP,非管理系统,用户行为日志感觉不是很必要的,但是,错误日志咱还是得记录则个.总不能上线后报bug了让自己手足无措吧,虽然不管有木有错误日志报bug都是件很头疼的事... 我们知 ...

  5. 4.28 前端开发日报

    给 「前端开发博客」 加星标,每天打卡学习 长按二维码即可识别"进入网页"查看哟~ 1.一名[合格]前端工程师的自检清单 前端开发是一个非常特殊的行业,它的历史实际上不是很长,但是 ...

  6. c# AttributeUsage的自定义属性和反射的一些基本用法

    定义一个AttributeUsage 定义一个Person属性上面声明AttributeUsage类 new Person转换成 PropertyInfo集合 通过GetCustomAttribute ...

  7. 介绍属性与自定义属性、AttributeUsage

    介绍属性                属性为访问自定义类型的注释信息提供通用的访问方式.注释信息是随意的,换句话说,这种信息不是语言自身固有的,而是由你自己能够想象到的任何信息.你能使用属性(att ...

  8. [AttributeUsage(AttributeTargets.Class)] 用法例子

    首先,创建一个自定义的Attribute,并且事先设定我们的Attribute将施加在class的元素上面以获取一个类代码的检查信息. using System; using System.Refle ...

  9. Ext.Net学习笔记22:Ext.Net Tree 用法详解

    上面的图片是一个简单的树,使用Ext.Net来创建这样的树结构非常简单,代码如下: <ext:TreePanel runat="server"><Root> ...

最新文章

  1. 数据分箱技术在Python中实现
  2. mac部署文件服务器,MAC 搭建本地服务器
  3. getSlotFromBufferLocked: unknown buffer: 0xf3d94ca0
  4. 2015蓝桥杯b组java_Java实现第十一届蓝桥杯JavaB组 省赛真题
  5. Kubernetes 环境搭建 - MacOS
  6. 树莓派该文件名_树莓派的20个常用命令
  7. PCL之计算点云质心---pcl::compute3DCentroid()
  8. STM32CUBEF4 实现USB 虚拟串口
  9. 海湾标准汉字码表查询_JBQGGST5000标准汉字码表
  10. 射极跟随器实验报告数据处理_射极跟随器实验报告 -
  11. 计算机Wor表格制作斜线表头,Word文档里怎么画表格斜线表头
  12. 汽车使用总结(七)--侧方停车
  13. 【input 身份证号】星号 代替,input 切割成 多个 小格格(类似)
  14. Android 中Native方法是怎样调用的
  15. 如何在 Android 上自定义来电通知?带有代码示例
  16. 手摸手教学之:梳理数据指标体系
  17. Android程序员的春天!Android项目开发如何设计整体架构?太香了
  18. Windows上下级目录
  19. Three.js物理材质MeshStandardMaterial和MeshPhysicalMaterial
  20. 在调用股票购买接口时要注意什么事项?

热门文章

  1. 论文:Representative Forgery Mining for Fake Face Detection
  2. 自己动手做QQ-特洛伊
  3. NOIP2011题解
  4. 【错误记录】自定义 Gradle 插件报错 ( Could not find implementation class x for plugin x specified in jar:file )
  5. Python -eventlet
  6. tensorflow RuntimeError:Graph is finalized and cannot be modified
  7. 知海匠库与学员温馨共度冬至迎平安夜
  8. Oracle中常见的Hint(一)
  9. PostgreSQL 实时位置跟踪+轨迹分析系统实践 - 单机顶千亿轨迹/天
  10. Python Turtle绘图[难度2星]:多边形螺旋线(颜色交叉/颜色分层)