【AttributeUsage】
System.AttributeUsage聲明一個Attribute的使用范圍與使用原則。
AllowMultiple 和 Inherited 參數是可選的,所以此代碼具有相同的效果:
AttributeTarget的值可以參考1。部分可取值如下:
如果 AllowMultiple 參數設置為 true,則返回特性可對單個實體應用多次。
如果 Inherited 設置為 false,則該特性不由從特性化的類派生的類繼承。
Attribute.GetCustomAttribute可以獲取一個類的Attribute。

[AttributeUsage(AttributeTargets.Class)] public class VersionAttribute : Attribute { public string Name { get; set; } public string Date { get; set; } public string Describtion { get; set; } } [Version(Name = "hyddd", Date = "2009-07-20", Describtion = "hyddd's class")] public class MyCode { //... } class Program { static void Main(string[] args) { var info = typeof(MyCode); var classAttribute = (VersionAttribute)Attribute.GetCustomAttribute(info, typeof(VersionAttribute)); Console.WriteLine(classAttribute.Name); Console.WriteLine(classAttribute.Date); Console.WriteLine(classAttribute.Describtion); } }
參考:
1、http://msdn.microsoft.com/zh-cn/library/system.attributetargets.aspx
2、http://msdn.microsoft.com/zh-cn/library/tw5zxet9.aspx
3、http://www.cnblogs.com/hyddd/archive/2009/07/20/1526777.html