【发布时间】:2017-05-12 03:37:05
【问题描述】:
我一直在尝试使用已在接口中声明的属性的属性。
假设:
[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; }
}
现在,根据我的阅读,继承=true 的 GetCustomAttribute() 应该返回继承的属性,但它看起来不起作用。
Attribute my = typeof(Child).GetProperty("y").GetCustomAttribute(typeof(My1Attribute), true); // my == null
为什么它不起作用?以及如何获取属性?
【问题讨论】:
标签: c# inheritance custom-attributes