C#的自定义属性AttributeUsage用法
[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用法相关推荐
- js第三节-自定义属性、索引值
js第三节-自定义属性.索引值 第一节陈明吕老师就大致提了一下自定义属性这个概念,自定义属性是我们自己给元素添加的属性,不是元素天生就存在的.自定义属性最常用的是索引值.举个例子来讲一下: 一.自定义 ...
- Unity自定义属性(Attribute)示例
一.自定义属性,使用场景 a.想知道当前的工程内定义过的特定的类 b.想知道当前工程内,类中定义过的特定的成员(变量,方法) 二.自定义属性介绍 a.需要继承自 System.Attrbute. 可以 ...
- Android基于Glide的二次封装,借鉴Glide思想二次封装Fresco
最近封装了个 fresco 的组件库:dfresco,就顺便来讲讲. 背景 fresco 图片库很强大,我们项目中就是使用的 fresco,但有一点就是,不怎么好使用,略麻烦.不同项目中,多多少少都需 ...
- .NET WebAPI 用ExceptionFilterAttribute实现错误(异常)日志的记录(log4net做写库操作)...
好吧,还是那个社区APP,非管理系统,用户行为日志感觉不是很必要的,但是,错误日志咱还是得记录则个.总不能上线后报bug了让自己手足无措吧,虽然不管有木有错误日志报bug都是件很头疼的事... 我们知 ...
- 4.28 前端开发日报
给 「前端开发博客」 加星标,每天打卡学习 长按二维码即可识别"进入网页"查看哟~ 1.一名[合格]前端工程师的自检清单 前端开发是一个非常特殊的行业,它的历史实际上不是很长,但是 ...
- c# AttributeUsage的自定义属性和反射的一些基本用法
定义一个AttributeUsage 定义一个Person属性上面声明AttributeUsage类 new Person转换成 PropertyInfo集合 通过GetCustomAttribute ...
- 介绍属性与自定义属性、AttributeUsage
介绍属性 属性为访问自定义类型的注释信息提供通用的访问方式.注释信息是随意的,换句话说,这种信息不是语言自身固有的,而是由你自己能够想象到的任何信息.你能使用属性(att ...
- [AttributeUsage(AttributeTargets.Class)] 用法例子
首先,创建一个自定义的Attribute,并且事先设定我们的Attribute将施加在class的元素上面以获取一个类代码的检查信息. using System; using System.Refle ...
- Ext.Net学习笔记22:Ext.Net Tree 用法详解
上面的图片是一个简单的树,使用Ext.Net来创建这样的树结构非常简单,代码如下: <ext:TreePanel runat="server"><Root> ...
最新文章
- 论文笔记 DNorm: disease name normalization with pairwise learning to rank
- Xml 格式数据的生成和解析
- mysql三范式_MySQL设计之三范式的理解
- java面试题4(基础)
- C语言结构体对齐的不足
- gridcontrol 控件的用法
- CG-CTF-Web-GBK Injection
- 【算法竞赛学习】二手车交易价格预测-Task2数据分析
- 只有3%的猿能完全看懂的高端漫画
- HTML script 标签
- java中双引号和单引号_Java中的单引号和双引号之间有区别吗?
- 采集练习(七) php 获得电视节目预告(一周节目)
- ORG LegacyCell for Mac - MS-20/Polysix音频合成器
- 报表工具Style Report报表打印功能
- html5弹性盒子模型,推荐10款弹性盒子源码(收藏)
- 轻量化网络ShuffleNet MobileNet v1/v2 解析
- java实现单词默写助手,强烈建议new-hand收藏
- OpenCV-Python官方教程-20-分水岭算法图像分割
- C语言零基础项目:打字母游戏!详细思路+源码分享
- 干货!教你4种文件恢复方法,再也不怕误删除!