首先要获取对象的类型,根据类型再获取Attribute,同一类型的对象获取到的Attribute是相同的。
static void Main(string[] args)
{
Type ty=typeof(System.Windows.Forms.Button);
foreach (Attribute attribute in ty.GetCustomAttributes(true))
{
Console.WriteLine(attribute);
}
Console.Read();
}
上述代码的结果如下:
System.Runtime.InteropServices.ComVisibleAttribute
System.Runtime.InteropServices.ClassInterfaceAttribute
System.Windows.Forms.SRDescriptionAttribute
System.ComponentModel.DesignerAttribute
System.ComponentModel.DesignerAttribute
System.Windows.Forms,Allow
System.ComponentModel.DefaultPropertyAttribute
System.ComponentModel.DefaultEventAttribute
System.ComponentModel.DesignerAttribute
System.ComponentModel.Design.Serialization.DesignerSerializerAttribute
System.ComponentModel.DesignerCategoryAttribute