1 namespace Common.Library.ChangeRecord 2 { 3 /// <summary> 4 /// desc:记录 修改记录 的特性 5 /// </summary> 6 [AttributeUsage((AttributeTargets)6140, AllowMultiple = true, Inherited = false)] 7 public class RecordAttribute : Attribute 8 { 9 private RecordType recordType; // 记录类型:更新/建立 10 private string author; // 做者 11 private DateTime date; // 更新/建立 日期 12 private string mark; // 备注 13 14 // 构造函数,构造函数的参数在特性中也称为“位置参数”。 15 public RecordAttribute(RecordType recordType, string author, string date) 16 { 17 this.recordType = recordType; 18 this.author = author; 19 this.date = Convert.ToDateTime(date); 20 } 21 22 // 对于位置参数,一般只提供get访问器 23 public RecordType RecordType { get { return recordType; } } 24 public string Author { get { return author; } } 25 public DateTime Date { get { return date; } } 26 27 // 构建一个属性,在特性中也叫“命名参数” 28 public string Mark 29 { 30 get { return mark; } 31 set { mark = value; } 32 } 33 } 34 /// <summary> 35 /// 记录类型 枚举 36 /// </summary> 37 public enum RecordType 38 { 39 Add, 40 Modify 41 } 42 }
NOTE:注意构造函数的参数 date,必须为一个常量、Type类型、或者是常量数组,因此不能直接传递DateTime类型。
这个类看上去和普通的类没有和大区别,显然不能它由于名字后面跟了个Attribute就摇身一变成了特性。它知足了,咱们前面所说的自定义特性的要求,继承了System.Attribute类、用AttributeUsage特性限制了应用的目标、有属性和构造函数等。
下面是应用特性:
1 namespace Common.Library 2 { 3 [Record(RecordType.Add, "Joey", "2013-07-23")] 4 [Record(RecordType.Modify, "Joey", "2013-07-23", Mark = "测试")] 5 [Record(RecordType.Modify, "Joey", "2013-07-24", Mark = "修改bug")] 6 public class TestAttribute 7 { 8 [Record(RecordType.Add, "Joey", "2013-07-23", Mark = "测试特性附加到方法上")] 9 [Record(RecordType.Add, "Shovy", "2013-07-25", Mark = "修改此方法")] 10 public void MyMethod() 11 { 12 } 13 14 [Record(RecordType.Add, "Joey", "2013-07-23", Mark = "方法2")] 15 [Record(RecordType.Add, "Zoe", "2013-07-25", Mark = "修改方法2")] 16 public void Method2() 17 { 18 } 19 } 20 }
1 namespace ConsoleApp 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 TestAttribute testClass = new TestAttribute(); 8 Type type = testClass.GetType(); //获取要得到自定义特性的类型的Type对象 9 Attribute[] myAttributes = Attribute.GetCustomAttributes(type);//获取类型上添加的自定义特性 10 Console.WriteLine("类型{0}上应用的Record特性:", type.ToString()); 11 Console.WriteLine(); 12 foreach (Attribute item in myAttributes) 13 { 14 if (item.GetType() == typeof(RecordAttribute)) //只打印咱自定义的特性 15 { 16 RecordAttribute attr = (RecordAttribute)item; 17 Console.WriteLine(" 类型:{0},更改人:{1},更改时间:{2},备注:{3}", attr.RecordType, attr.Author, attr.Date.ToString("yyyy-MM-dd"), attr.Mark); 18 } 19 } 20 Console.WriteLine(); 21 Console.WriteLine("类型{0}的方法上应用的Record特性:", type.ToString()); 22 Console.WriteLine(); 23 foreach (MethodInfo mInfo in type.GetMethods()) //遍历该类型的全部方法 24 { 25 if (Attribute.IsDefined(mInfo, typeof(RecordAttribute))) //只有在方法上附加了属性,才遍历 26 { 27 Console.WriteLine(" 类型{0}的{1}方法上的Record特性", type.ToString(), mInfo.ToString()); 28 Console.WriteLine(); 29 foreach (Attribute item in Attribute.GetCustomAttributes(mInfo)) //遍历方法上的 特性 30 { 31 if (item.GetType() == typeof(RecordAttribute)) 32 { 33 RecordAttribute attr = (RecordAttribute)item; 34 Console.WriteLine(" 类型:{0},更改人:{1},更改时间:{2},备注:{3}", attr.RecordType, attr.Author, attr.Date.ToString("yyyy-MM-dd"), attr.Mark); 35 } 36 } 37 Console.WriteLine(); 38 } 39 } 40 } 41 }}
运行效果以下图所示:
获取自定义特性的方法二:
首先基于类型(本例中是DemoClass)获取一个Type对象,而后调用Type对象的GetCustomAttributes()方法,获取应用于该类型上的特性。
当指定GetCustomAttributes(Type attributeType, bool inherit) 中的第一个参数attributeType时,
将只返回指定类型的特性,不然将返回所有特性;第二个参数指定是否搜索该成员的继承链以查找这些属性。
代码以下:
1 namespace ConsoleApp 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 TestAttribute testClass = new TestAttribute(); 8 Type type = testClass.GetType(); 9 object[] records = type.GetCustomAttributes(typeof(RecordAttribute), false); //获取类型上的特性 10 Console.WriteLine("类型{0}上应用的Record特性:", type.ToString()); 11 Console.WriteLine(); 12 foreach (RecordAttribute record in records) 13 { 14 Console.WriteLine(" 类型:{0},更改人:{1},更改时间:{2},备注:{3}", record.RecordType, record.Author, record.Date.ToString("yyyy-MM-dd"), record.Mark); 15 } 16 Console.WriteLine(); 17 Console.WriteLine("类型{0}的方法上应用的Record特性:", type.ToString()); 18 Console.WriteLine(); 19 foreach (MethodInfo mInfo in type.GetMethods()) //遍历该类型的全部方法 20 { 21 if (Attribute.IsDefined(mInfo, typeof(RecordAttribute))) //只有在方法上附加了属性,才遍历 22 { 23 Console.WriteLine(" 类型{0}的{1}方法上的Record特性", type.ToString(), mInfo.ToString()); 24 Console.WriteLine(); 25 foreach (RecordAttribute record in mInfo.GetCustomAttributes(typeof(RecordAttribute), false)) //遍历方法上的 特性 26 { 27 Console.WriteLine(" 类型:{0},更改人:{1},更改时间:{2},备注:{3}", record.RecordType, record.Author, record.Date.ToString("yyyy-MM-dd"), record.Mark); 28 } 29 Console.WriteLine(); 30 } 31 } 32 } 33 } 34 }
两种方法运行的结果是同样的,如上图。这两种方法的区别在与,第一种方法是用的Atrribute基类的静态方法获取对应类型的自定义特性的集合。而第二种方案是使用的Type类型的方法获取的。
demo下载:点击这里下载代码
资料:
http://www.cnblogs.com/JimmyZhang/archive/2008/01/27/1055254.htmlhtml
Attribute在.net编程中的应用(一)
Attribute在.net编程中的应用(二)
Attribute在.net编程中的应用(三)
Attribute在.net编程中的应用(四)
Attribute在.net编程中的应用(五)