Why can't you create a Generic Class that inherits from Attribute in C#?

Today I was creating a set of attributes for a project I'm working on and I attempted to create a generic class that inherits from Attribute and I didn't think anything more about it until I compiled and got the error: "error CS0698: A generic type cannot derive from 'Attribute' because it is an attribute class".

I thought this was kind of strange, so just out of curiosity I opened up the C# 2.0 Specification and sure enough in section 20.5.9 it forbids this very thing.

20.5.9 Attributes
An open type may not be used anywhere inside an attribute. A closed constructed type can be 
used as the argument to an attribute, but cannot be used as the attribute-name, because System.Attribute
cannot be the base type of a generic class declaration.
class A: Attribute 
{
public A(Type t) {...}
}
class B<T>: Attribute {} // Error, cannot use Attribute as base
class List<T>
{
[A(typeof(T))] T t; // Error, open type in attribute
}
class X
{
[A(typeof(List<int>))] int x; // Ok, closed constructed type
[B<int>] int y; // Error, invalid attribute name
}

Now I know Attribute is special but I'm puzzled as to why it can't handle open types. There must be some (and I'm sure there are many) implementation details that I don't understand.

Can anyone explain to me why a generic class declaration may not use Attribute as a direct or indirect base class? (Besides the obvious, because they say so in the specification :)

6 Comments

Comments have been disabled for this content.