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 :)

Published Tuesday, October 12, 2004 6:36 PM by puzzlehacker

Comments

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

Could it be because generic types are not CLS compliant?

Tuesday, October 12, 2004 9:02 PM by David M. Kean

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

Thats possible I didn't think of that but I think there has to be a more technicial reason.

Wednesday, October 13, 2004 11:41 AM by Wes

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

So, it there explanation?

Tuesday, July 31, 2007 1:43 PM by Alex Blokha

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

I think it's a problem of language architecture.

If you would use something like MyAttribute(Of T) in VB you would code weird stuff like this:

<MyAttribute(Of MyType)("MyParameter")>

Tuesday, December 23, 2008 9:42 PM by test1

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

Generic parameters are resolved at runtime?

Tuesday, November 16, 2010 5:57 PM by cosmo

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

Run into the same problem:

public abstract class Validate : Attribute

   {

       public abstract bool IsValid();

   }

   public class ValidateGreaterThan<T> : Validate

   {

       private readonly Func<T, bool> _predicate;

       public ValidateGreaterThan(Func<T, bool> predicate)

       {

           _predicate = predicate;

       }

       public T PropertyValue

       {

           protected get { return PropertyValue; }

           set { PropertyValue = (T)Convert.ChangeType(value, typeof(T)); }

       }

       public override bool IsValid()

       {

           return _predicate.Invoke(PropertyValue);

       }

   }

Tuesday, March 15, 2011 6:13 AM by Lukas