using System;
using System.Collections.Generic;
using System.Text;

namespace 自定义特性
{
//1.也行类的后缀以Attribute结尾
//2.需要继承自:System.Attribute
//3.一般情况下声明为 sealed(不需要被继承)
//4.一般情况下特性类用来表示目标结构的一些状态(定义一些字段/属性,一般不需要定义方法)
[AttributeUsage(AttributeTargets.Class)]//表示特性类的使用目标,(表示特性类可以应用到程序的那些结构)
sealed class MyTestAttribute :System.Attribute
{
public string Description { get; set; }
public string VersionNumber { get; set; }
public int ID { get; set; }

    public MyTestAttribute(string des)
    {
        Description = des;
    }
}

}
//---------------------------------使用---------------------
using System;

namespace 自定义特性
{
[MyTest("自定义特性类",ID = 100)]
class Program
{
static void Main(string[] args)
{
Type type = typeof(Program);//通过typeof+类名,获取类的type对象
object[] array = type.GetCustomAttributes(false);//获取类的所有特性(false)不包括父类的
MyTestAttribute mytest = array[0] as MyTestAttribute;
Console.WriteLine(mytest.Description);
Console.WriteLine(mytest .ID );
}
}
}