You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fundamentals/reflection/how-to-examine-and-instantiate-generic-types-with-reflection.md
+17-32Lines changed: 17 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,13 @@
1
1
---
2
2
title: "How to: Examine and Instantiate Generic Types with Reflection"
3
3
description: See how to examine and instantiate generic types with reflection. Use the IsGenericType, IsGenericParameter, and GenericParameterPosition properties.
4
-
ms.date: "03/30/2017"
4
+
ms.date: 03/19/2025
5
5
dev_langs:
6
6
- "csharp"
7
7
- "vb"
8
-
- "cpp"
9
8
helpviewer_keywords:
10
9
- "reflection, generic types"
11
10
- "generics [.NET], reflection"
12
-
ms.assetid: f93b03b0-1778-43fc-bc6d-35983d210e74
13
11
---
14
12
# How to: Examine and instantiate generic types with reflection
15
13
@@ -19,52 +17,44 @@ You can create a <xref:System.Type> object that represents a constructed type by
19
17
20
18
## To examine a generic type and its type parameters
21
19
22
-
1. Get an instance of <xref:System.Type> that represents the generic type. In the following code, the type is obtained using the C# `typeof` operator (`GetType` in Visual Basic, `typeid` in Visual C++). See the<xref:System.Type>class topic for other ways to get a <xref:System.Type> object. Note that in the rest of this procedure, the type is contained in a method parameter named `t`.
20
+
1. Get an instance of <xref:System.Type> that represents the generic type. In the following code, the type is obtained using the C# `typeof` operator (`GetType` in Visual Basic). For other ways to get a<xref:System.Type>object, see <xref:System.Type>. In the rest of this procedure, the type is contained in a method parameter named `t`.
2. Use the <xref:System.Type.IsGenericType%2A> property to determine whether the type is generic, and use the <xref:System.Type.IsGenericTypeDefinition%2A> property to determine whether the type is a generic type definition.
4. For each type argument, determine whether it is a type parameter (for example, in a generic type definition) or a type that has been specified for a type parameter (for example, in a constructed type), using the <xref:System.Type.IsGenericParameter%2A> property.
5. In the type system, a generic type parameter is represented by an instance of <xref:System.Type>, just as ordinary types are. The following code displays the name and parameter position of a <xref:System.Type> object that represents a generic type parameter. The parameter position is trivial information here; it is of more interest when you are examining a type parameter that has been used as a type argument of another generic type.
40
+
5. In the type system, a generic type parameter is represented by an instance of <xref:System.Type>, just as ordinary types are. The following code displays the name and parameter position of a <xref:System.Type> object that represents a generic type parameter. The parameter position is trivial information here; it is of more interest when you're examining a type parameter that's been used as a type argument of another generic type.
6. Determine the base type constraint and the interface constraints of a generic type parameter by using the <xref:System.Type.GetGenericParameterConstraints%2A> method to obtain all the constraints in a single array. Constraints are not guaranteed to be in any particular order.
7. Use the <xref:System.Type.GenericParameterAttributes%2A> property to discover the special constraints on a type parameter, such as requiring that it be a reference type. The property also includes values that represent variance, which you can mask off as shown in the following code.
8. The special constraint attributes are flags, and the same flag (<xref:System.Reflection.GenericParameterAttributes.None?displayProperty=nameWithType>) that represents no special constraints also represents no covariance or contravariance. Thus, to test for either of these conditions you must use the appropriate mask. In this case, use <xref:System.Reflection.GenericParameterAttributes.SpecialConstraintMask?displayProperty=nameWithType> to isolate the special constraint flags.
55
+
8. The special constraint attributes are flags, and the same flag (<xref:System.Reflection.GenericParameterAttributes.None?displayProperty=nameWithType>) that represents no special constraints also represents no covariance or contravariance. Thus, to test for either of these conditions, you must use the appropriate mask. In this case, use <xref:System.Reflection.GenericParameterAttributes.SpecialConstraintMask?displayProperty=nameWithType> to isolate the special constraint flags.
@@ -73,26 +63,22 @@ A generic type is like a template. You cannot create instances of it unless you
73
63
74
64
1. Get a <xref:System.Type> object that represents the generic type. The following code gets the generic type <xref:System.Collections.Generic.Dictionary%602> in two different ways: by using the <xref:System.Type.GetType%28System.String%29?displayProperty=nameWithType> method overload with a string describing the type, and by calling the <xref:System.Type.GetGenericTypeDefinition%2A> method on the constructed type `Dictionary\<String, Example>` (`Dictionary(Of String, Example)` in Visual Basic). The <xref:System.Type.MakeGenericType%2A> method requires a generic type definition.
2. Construct an array of type arguments to substitute for the type parameters. The array must contain the correct number of <xref:System.Type> objects, in the same order as they appear in the type parameter list. In this case, the key (first type parameter) is of type <xref:System.String>, and the values in the dictionary are instances of a class named `Example`.
4. Use the <xref:System.Activator.CreateInstance%28System.Type%29> method overload to create an object of the constructed type. The following code stores two instances of the `Example` class in the resulting `Dictionary<String, Example>` object.
@@ -105,8 +91,7 @@ The code example defines a set of test types, including a generic type that illu
105
91
106
92
The example constructs a type from the <xref:System.Collections.Generic.Dictionary%602> class by creating an array of type arguments and calling the <xref:System.Type.MakeGenericType%2A> method. The program compares the <xref:System.Type> object constructed using <xref:System.Type.MakeGenericType%2A> with a <xref:System.Type> object obtained using `typeof` (`GetType` in Visual Basic), demonstrating that they are the same. Similarly, the program uses the <xref:System.Type.GetGenericTypeDefinition%2A> method to obtain the generic type definition of the constructed type, and compares it to the <xref:System.Type> object representing the <xref:System.Collections.Generic.Dictionary%602> class.
0 commit comments