开发者

How to get the attributes override properties?

Does not work Attribute.GetCustomAttribute:

using System;

class Program
{
    static void Main()
    {
        var p = typeof(MyClass2).GetProperty("Value");
        var a = Attribute.GetCustomAttribute(p, typeof(ObsoleteAttribute), true);
     开发者_开发技巧   Console.WriteLine(a != null);
    }
}

public class MyClass
{
    [Obsolete]
    public virtual string Value { get; set; }
}

public class MyClass2 : MyClass
{
    public override string Value { get; set; }
}

Output: False

Why?


If you look at the docs for ObsoleteAttribute you'll see that its AttributeUsage sets Inherited to false - so the attribute isn't inherited by overriding members.

I suspect if you check that it's overriding a base property and work your way up the inheritance chain, you'll be able to discover the attribute that way. It's a bit of a mess doing this sort of thing, admittedly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜