How to get attribute override properties?

Attribute.GetCustomAttribute does not work:

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?

+4
source share
1 answer

If you look at the documents for ObsoleteAttribute , you will see that its AttributeUsage sets Inherited to false, so the attribute is not inherited when overriding elements.

I suspect that if you verify that it overrides the underlying property and works on the inheritance chain, you can detect the attribute this way. In truth, this is confusion.

+4
source

Source: https://habr.com/ru/post/1369614/


All Articles