=================================AttributeUsage类
属性: AllowMultiple 指定是否可以在某个类或者方法中存在多个特性
例如在方法上有多个特性
[Developer("李四", "项目主管", Yesno = true)]
[Developer("王五", "项目经理", Yesno = true)]
public void aa()
{}
继承属性: Inherited <?xml:namespace prefix="[default]" ns="http://www.w3.org/1999/xhtml">?xml:namespace>假如有一个类继承自我们的DemoClass,那么当我们将RecordAttribute添加到DemoClass上时,DemoClass的子类也会获得该特性。而当特性应用于一个方法,如果继承自该类的子类将这个方法覆盖,那么Inherited则用于说明是否子类方法是否继承这个特性。
类: AttributeTargets
=================================AttributeTargets
publicenumAttributeTargets{
Assembly = 1, //可以对程序集应用属性。 Module = 2, //可以对模块应用属性。 Class = 4, //可以对类应用属性。 Struct = 8, //可以对结构应用属性,即值类型。 Enum = 16, //可以对枚举应用属性。 Constructor = 32, //可以对构造函数应用属性。 Method = 64, //可以对方法应用属性。 Property = 128, //可以对属性 (Property) 应用属性 (Attribute)。 Field = 256, //可以对字段应用属性。 Event = 512, //可以对事件应用属性。 Interface = 1024, //可以对接口应用属性。 Parameter = 2048, //可以对参数应用属性。 Delegate = 4096, //可以对委托应用属性。 ReturnValue = 8192, //可以对返回值应用属性。 GenericParameter = 16384, //可以对泛型参数应用属性。 All = 32767, //可以对任何应用程序元素应用属性。}
=================================自定义特性(DeveloperAttribute类)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Runtime.InteropServices;namespace WindowsFormsApplication2{[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]public class DeveloperAttribute:Attribute{private string _name;private string _level;private bool yesno;public string Name{get { return _name; }}public string Level{get { return _level; }}//属性public bool Yesno{get { return yesno; }set { yesno = value; }}//构造函数public DeveloperAttribute(string name,string level){this._name = name;this._level = level;this.yesno = false;}}}
=================================特性的使用(必须在共有类型上使用)
using System.Runtime.InteropServices;
1,在类上使用
[Developer("李四", "项目主管", Yesno = true)]
public class aa
{ }
2,在方法上使用
[Developer("李四", "项目主管", Yesno = true)]
public void aa()
{}
=================================检索储存在特性中的信息
1,检索存在类上的特性的信息
[Developer("张三", "项目主管", Yesno = true)][Developer("李四", "项目主管", Yesno = true)]public class aa{}
private void button1_Click(object sender, EventArgs e){//第一种检索方式(检索一个)//DeveloperAttribute d= (DeveloperAttribute )Attribute.GetCustomAttribute(typeof(aa), typeof(DeveloperAttribute));//MessageBox.Show(d.ToString() + "," + d.Name);//第二中检索方式(检索一个或多个)DeveloperAttribute[] de = (DeveloperAttribute[])Attribute.GetCustomAttributes(typeof(aa), typeof(DeveloperAttribute));foreach (DeveloperAttribute d in de){MessageBox.Show(d.Name);}//第三种检索方式(检索一个或多个)//Type t = typeof(aa);//object[] o = t.GetCustomAttributes(typeof(DeveloperAttribute), false);//foreach (DeveloperAttribute d in o)//{// MessageBox.Show(d.ToString()+","+d.Name);//}}
2,检索存在方法上的特性的信息
[Developer("张迪", "项目经理", Yesno = true)][Developer("张迪", "项目经理", Yesno = true)]public void getStr(){}
private void button2_Click(object sender, EventArgs e){Type t = typeof(Form1);MethodInfo[] m = t.GetMethods();for (int i = 0; i < m.Length; i++){//第一种检索方式(检索一个)DeveloperAttribute a = (DeveloperAttribute)Attribute.GetCustomAttribute(m[i], typeof(DeveloperAttribute));if(a!=null)MessageBox.Show(m[i].ToString() + "," + a.Name);//第二中检索方式(检索一个或多个)//DeveloperAttribute[] a = (DeveloperAttribute[])Attribute.GetCustomAttributes(m[i], typeof(DeveloperAttribute));//foreach (DeveloperAttribute aa in a)//{// MessageBox.Show(m[i].ToString() + "," + aa.Name);//}}}