In answer to this question I tried to use Type.GetCustomAttributes(true)
on a class which implements an interface which has an Attribute defined on it. I was surprised to discover that GetCustomAttributes
didn't return the attribute defined on the interface. Why doesn't it? Aren't interfaces part of the inheritance chain?
Sample code:
[Attr()]
public interface IInterface { }
public class DoesntOverrideAttr : IInterface { }
class Program
{
static void Main(string[] args)
{
foreach (var attr in typeof(DoesntOverrideAttr).GetCustomAttributes(true))
Console.WriteLine("DoesntOverrideAttr: " + attr.ToString());
}
}
[AttributeUsage(AttributeTargets.All, Inherited = true)]
public class Attr : Attribute
{
}
Outputs: Nothing
I don't believe attributes defined on implemented interfaces can be reasonably inherited. Consider this case:
[AttributeUsage(Inherited=true, AllowMultiple=false)]
public class SomethingAttribute : Attribute {
public string Value { get; set; }
public SomethingAttribute(string value) {
Value = value;
}
}
[Something("hello")]
public interface A { }
[Something("world")]
public interface B { }
public class C : A, B { }
Since the attribute specifies that multiples are not allowed, how would you expect this situation to be handled?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With