Generics and Attributes

Attributes can be applied to generic types in the same way as nongeneric types. However, you can apply attributes only on open generic types and closed constructed generic types, not on partially constructed generic types. An open generic type is one where none of the type arguments are specified, such as Dictionary<TKey, TValue> A closed constructed generic type specifies all type arguments, such as Dictionary<string, object>. A partially constructed generic type specifies some, but not all, type arguments. An example is Dictionary<string, TValue>.

The following examples use this custom attribute:

class CustomAttribute : Attribute
{
    public object? info;
}

An attribute can reference an open generic type:

public class GenericClass1<T> { }

[CustomAttribute(info = typeof(GenericClass1<>))]
class ClassA { }

Specify multiple type parameters using the appropriate number of commas. In this example, GenericClass2 has two type parameters:

public class GenericClass2<T, U> { }

[CustomAttribute(info = typeof(GenericClass2<,>))]
class ClassB { }

An attribute can reference a closed constructed generic type:

public class GenericClass3<T, U, V> { }

[CustomAttribute(info = typeof(GenericClass3<int, double, string>))]
class ClassC { }

An attribute that references a generic type parameter causes a compile-time error:

[CustomAttribute(info = typeof(GenericClass3<int, T, string>))]  //Error CS0416
class ClassD<T> { }

Beginning with C# 11, a generic type can inherit from Attribute:

public class CustomGenericAttribute<T> : Attribute { }  //Requires C# 11

To obtain information about a generic type or type parameter at run time, you can use the methods of System.Reflection. For more information, see Generics and Reflection.

See also