我一直在嘗試使用已經在接口中聲明的屬性的屬性。使用來自接口的繼承屬性的屬性
假設:現在
[AttributeUsage(AttributeTargets.Property, Inherited=true)]
class My1Attribute : Attribute
{
public int x { get; set; }
}
interface ITest
{
[My1]
int y { get; set; }
}
class Child : ITest
{
public Child() { }
public int y { get; set; }
}
,從我讀,GetCustomAttribute()與繼承=真應該返回繼承的屬性,但看起來這是行不通的。
Attribute my = typeof(Child).GetProperty("y").GetCustomAttribute(typeof(My1Attribute), true); // my == null
爲什麼它不工作?以及如何獲得屬性?