开发者

In .Net, why aren't attributes declared on interfaces returned when calling Type.GetCustomAttributes(true)?

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,开发者_JAVA技巧 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?


Because the type DoesntOverrideAttr doesn't have any custom attributes. The Interface that it implements does (remember, a class doesn't inherit from an interface...it implements it so getting attributes up the inheritance chain still won't include attributes from interfaces):

// This code doesn't check to see if the type implements the interface.
// It should.
foreach(var attr in typeof(DoesntOverrideAttr)
                        .GetInterface("IInterface")
                        .GetCustomAttributes(true))
{
    Console.WriteLine("IInterface: " + attr.ToString());
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜