Substitutes the elements of an array of types for the type parameters of the current generic type definition, and returns a Type object representing the resulting constructed type.
The current type shall be a generic type definition.
- typeArguments
- Documentation for this section has not yet been entered.
A Type representing the constructed type formed by substituting the elements of typeArguments for the type parameters of the current generic type definition.
Type Reason ArgumentException The number of elements in typeArguments is not the same as the number of type parameters of the current generic type definition.
-or-
An element of typeArguments does not satisfy the constraints specified for the corresponding type parameter of the current generic type definition.
ArgumentNullException typeArguments is null.
-or-
An element of typeArguments is null.
InvalidOperationException The current type does not represent the definition of a generic type. That is, System.Type.IsGenericTypeDefinition returns false.
This method allows you to write code that assigns specific types to the type parameters of a generic type definition, thus creating a Type object that represents a particular constructed type. You can use this Type object to create runtime instances of the constructed type.
The Type object returned by this method is the same as that obtained by calling the object.GetType method of the resulting constructed type, or the object.GetType method of any constructed type that was created from the same generic type using the same type arguments.
Note: An array type whose element type is a generic type is not itself a generic type. Thus, you cannot call this method to bind an array type. To bind a type argument to this type, call Type.GetElementType to obtain the generic type, then this method to bind the type argument to the generic type, and, finally, Type.MakeArrayType to create the array type.For a list of the invariant conditions for terms used in generic reflection, see the Type.IsGenericType property description.
The following example uses Type.GetType and Type.MakeGenericType to create a constructed type from the generic Dictionary type. The constructed type represents a Dictionary of Test objects with string keys.
C# Example
using System; using System.Reflection; using System.Collections.Generic; public class Test { public static void Main() { Console.WriteLine("\n--- Create a constructed type from the generic Dictionary type."); // Create a type object representing the generic Dictionary // type. Type generic = Type.GetType("System.Collections.Generic.Dictionary"); DisplayTypeInfo(generic); // Create an array of types to substitute for the type // parameters of Dictionary. The key is of type string, and // the type to be contained in the Dictionary is Test. Type[] typeArgs = { typeof(string), typeof(Test) }; Type constructed = generic.MakeGenericType(typeArgs); DisplayTypeInfo(constructed); // Compare the type objects obtained above to type objects // obtained using typeof() and GetGenericTypeDefinition(). Console.WriteLine("\n--- Compare types obtained by different methods:"); Type t = typeof(Dictionary<string, Test>); Console.WriteLine("\tAre the constructed types equal? {0}", t == constructed); Console.WriteLine("\tAre the generic types equal? {0}", t.GetGenericTypeDefinition() == generic); } private static void DisplayTypeInfo(Type t) { Console.WriteLine("\n{0}", t); Console.WriteLine("\tIs this a generic type definition? {0}", t.IsGenericTypeDefinition); Console.WriteLine("\tDoes it have generic type arguments? {0}", t.HasGenericArguments); Type[] typeArguments = t.GetGenericArguments(); Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length); foreach (Type tParam in typeArguments) { Console.WriteLine("\t\t{0}", tParam); } } } /* This example produces the following output: --- Create a constructed type from the generic Dictionary type. System.Collections.Generic.Dictionary[KeyType,ValueType] Is this a generic type definition? True Does it have generic type arguments? True List type arguments (2): K V System.Collections.Generic.Dictionary[System.String, Test] Is this a generic type definition? False Does it have generic type arguments? True List type arguments (2): System.String Test --- Compare types obtained by different methods: Are the constructed types equal? True Are the generic types equal? True */
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0