特性类
[AttributeUsage(AttributeTargets.Class)]//该特性类可以应用到的程序结构有哪些 MyTestAttribute:system.Attribute{ public string Description{get;set;} public string VersionNumber{get;set;} public int ID{get;set;} public MyAttribute(string desc) { cw (this.Description=desc) } } 1.以Attribute结尾 2.需要继承自System.Attribute 3.一般情况下声明为sealed 4.一般情况下表示目标结构的一些状态 使用的时候 [MyTest(“简单的特性类”,ID=100)]//此处胜省略了Attribute,指定属性的名字给属性命名,为参数命名 class Program { Type type=typeof(Program);
Object[] array=type.getCustomAttribute(false);//表示是否搜索他的父类的特性
MytestAttribute mytest=array[0] as MyTestAttribute;
Console.writeLine(myTest.ID);
}