发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
先贴定义的特性类的代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace AttributeTest { [Serializable] [ComVisible(true)] /*AttributeUsage顾名思义:这个特性用在什么地方,是用在类上呢,还是用在方法上,仰或是程序集上? *能用在什么地方是其参数(AttributeTargets)决定滴(为什么?)。 * ArributeTargets是个枚举,正如下面一行代码所示,指定了RecordAttributes类(因为其继承了Attribute),做为特性用在Class和Method上。 * AllowMultiple参数呢?顾名思义是可以用于多行,不明白?好吧看看这个: [Record("小小威", "修改了该类改动XXX", "2013-7-18 14:24:32")] [Record("记忆中的马肠河", "修改了该类改动XXX", "2013-8-18 18:24:32")] 假如我们这个特性类创建好了,而且指定了AllowMultiple=true,就可以像这样(多行)用在类上或者方法上了。 */ [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple = true)] //特性类要继承自Attribute类(为什么?),.net约定特性类要以Attribute做结尾 public class RecordAttribute : Attribute { //开发者 public string Name { get; set; } //备注 public string Memo { get; set; } //日期 public DateTime Date { get; set; } //定义构造,RecordAttribute就是一个名符其实的类,因为其继承了Attribute,并且在其前面设定了特定,所以自己也就可以做为特性来用了。 public RecordAttribute(string name, string memo, string date) { this.Name = name; this.Memo = memo; this.Date =Convert.ToDateTime(date); } } }
然后是如何用该特性:
#region 反射 特性 //用于测试自定义的特性(Attribute)类 AttributeTest att = new AttributeTest(); //type类是实现反射的关键 Type t = att.GetType(); //利用反射获取AttributeTest类的特性,并打印 object[] os = t.GetCustomAttributes(typeof(RecordAttribute), false); Console.WriteLine("AttributeTest的特性信息:"); foreach (RecordAttribute item in os) { Console.WriteLine("操作员:" + item.Name + "," + "备注:" + item.Memo + "," + "日期:" + item.Date); } Console.WriteLine("AttributeTest的特性信息结束"); Console.WriteLine("AttributeTest的aa方法的特性信息:"); MethodInfo method = t.GetMethod("aa"); object[] mAtt = method.GetCustomAttributes(typeof(RecordAttribute), false); foreach (RecordAttribute item in mAtt) { Console.WriteLine("操作员:" + item.Name + "," + "备注:" + item.Memo + "," + "日期:" + item.Date); } Console.WriteLine("AttributeTest的aa方法的特性结束"); #endregion
新建控制台程序,在program中引用AttributeTest,在main中拷贝该代码就可以直接运行。
用反射来获取类或者方法的特性的方式,能让我们更方便地获取代码的变更历史,相比较于用注释的方式,确实好多了。
来自: 昵称10504424 > 《Asp.Net》
0条评论
发表
请遵守用户 评论公约
C# 特性(Attribute) | 菜鸟教程
C# 特性(Attribute) | 菜鸟教程C# 特性(Attribute)规定特性(Attribute)声明自定义特性构建自定义特性在目标程序元素上应用自定义特性通过反射访问特性。// 一个自定义特性 BugFix 被赋给类及其成...
.Net 中的反射(反射特性)
Net 中的反射(反射特性)} // 对于位置参数,通常只提供get访问器 public string RecordType { get { return recordType; } }...
[你必须知道的.NET] 第三回:历史纠葛:特性和属性
Attributes in c#
亦即我们可以定义自定义属性类的属性。}//即便我们不想我们的属性用户设置Version这个数据属性,我们也不得不提供set方法set{this.verion = value;}}}[Help("This is Class1")]public class ...
C#特性详解
C#特性详解。AttributeUsage has a named parameter, AllowMultiple, with which you can make a custom attribute single-use or multiuse." data-guid="87bd32317ca4e710362d8800ed5ef3fd&...
有关C#标签Attribute的熟悉
3、Attribute类除了.NET提供的那些Attribute派生类之外,我们可以自定义我们自己的Attribute,所有自定义的Attribute必须从Attribute类派生。这里需要指出的是Attribute的命名规范,也就是你的Attribut...
C# 中特性(Attribute)的使用简介
Attribute(特性)MSDN给出的定义: Attribute 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。特性定义如下:?Prope...
.Net基础之特性
Net基础之特性。public class TMessgAttribute : Attribute//1.定义类TMessg加上后缀TMessgAttribute 2.继承Attribute。public TMessgAt...
.net Attribute的读书笔记
Attribute 8 { 9 public ErrorAttribute(string date, string author, string errormessage)10 {11 this.//获取类中的attribute foreach (ErrorAttribute ...
微信扫码,在手机上查看选中内容