Attribute
[AttributeTargets_All]
public TestAttributeClass()
{
}
[Obsolete("該方法已經停用!請使用Method2")]
public void Method1()
{
}
即以一箇中括號包含進來的一些特殊的註解屬性,來修飾類,方法,字段,屬性。這些屬性可以是.net自帶的,也可以是用戶自定義出來的。 //AttributeTargets指定對哪些程序元素使用(如這裏是針對類)
[AttributeUsage(AttributeTargets.Class)]
public class AttributeTargets_ClassAttribute : Attribute
{
}
//限制用在方法上面
[AttributeUsage(AttributeTargets.Method)]
public class AttributeTargets_MethodAttribute : Attribute
{
public AttributeTargets_MethodAttribute(string ctorStr)
{
this.testProperty = ctorStr;
}
public string testProperty { get; set; }
}
//限制用在方法和類上面
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Class)]
public class AttributeTargets_MethodOrClassAttribute : Attribute
{
}
//所有元素上都可以使用
[AttributeUsage(AttributeTargets.All)]
public class AttributeTargets_AllAttribute : Attribute
{
}
創建一個測試類:
[AttributeTargets_Class]
[AttributeTargets_MethodOrClass]
[AttributeTargets_All]
class TestAttributeClass
{
[AttributeTargets_All]
public TestAttributeClass()
{
}
[Obsolete("該方法已經停用!請使用Method2")]
public void Method1()
{
}
[AttributeTargets_Method("just for testing!")]
public void Method2()
{
}
[AttributeTargets_MethodOrClass]
public void Method3()
{
}
[AttributeTargets_All]
public string MyProperty1 { get; set; }
[AttributeTargets_All]
public string MyProperty2 { get; set; }
[AttributeTargets_All]
public int MyFiled1 = 0;
[AttributeTargets_All]
public string MyFiled2 = "test";
}
static void Main(string[] args)
{
Type classType = typeof(TestAttributeClass);
Console.WriteLine("下面是獲得所有包含attribute的屬性:");
foreach (PropertyInfo pro in classType.GetProperties())
{
foreach (Attribute attr in Attribute.GetCustomAttributes(pro))
{
Console.WriteLine("Property:{0} have attribute:{1}", pro.Name, attr.ToString());
}
Console.WriteLine("---------------------------");
}
Console.WriteLine("下面獲得是所有包含attribute的字段:");
foreach (FieldInfo field in classType.GetFields())
{
foreach (Attribute attr in Attribute.GetCustomAttributes(field))
{
Console.WriteLine("Field:{0} have attribute:{1}", field.Name, attr.ToString());
}
Console.WriteLine("---------------------------");
}
Console.WriteLine("下面是獲得所有包含AttributeTargets_MethodAttribute註解屬性的方法及attribute中的屬性值:");
foreach (MethodInfo method in classType.GetMethods())
{
foreach (Attribute attr in Attribute.GetCustomAttributes(method))
{
//Console.WriteLine("Method:{0} have attribute:{1}",method.Name,attr.ToString());
if (attr.GetType() == typeof(AttributeTargets_MethodAttribute))
{
//Console.WriteLine("Method:{0} have attribute:{1}", method.Name, attr.ToString());
Console.WriteLine("Method:{0} have attribute:{1}", method.Name, ((AttributeTargets_MethodAttribute)attr).testProperty);
}
}
Console.WriteLine("---------------------------");
}
Console.WriteLine("下面是獲得Method1中ObsoleteAttribute註解屬性的只讀屬性Message:");
ObsoleteAttribute obs =
(ObsoleteAttribute)Attribute.GetCustomAttribute(classType.GetMethod("Method1"), typeof(ObsoleteAttribute));
Console.WriteLine("Method1 have attribute message:{0}",obs.Message);
}
編譯運行,結果是: