Professional Documents
Culture Documents
Generics and Attributes - C# - Microsoft Learn
Generics and Attributes - C# - Microsoft Learn
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> .
C# Copy
C# Copy
[CustomAttribute(info = typeof(GenericClass1<>))]
class ClassA { }
Specify multiple type parameters using the appropriate number of commas. In this example, GenericClass2 has two type parameters:
C# Copy
[CustomAttribute(info = typeof(GenericClass2<,>))]
class ClassB { }
C# Copy
C# Copy
C# Copy
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.