Overrides the element of the type array that consists of the type parameters defined by the current generic type, and returns a Type object that represents the resulting constructed type.
namespaces: System
Assembly: mscorlib (in mscorlib.dll)
Public Virtual Type MakeGenericType ( params type[] typearguments)
Parameters
TypeArguments
An array of types that will replace the type parameters of the current generic type.
return value
Type:System.Type
The type representation of a construct is formed by substituting the typearguments element for the type parameter of the current generic type.
Note
The MakeGenericType method allows you to write code that assigns a specific type to a generic type definition, thereby creating a type parameter that represents an object of a particular constructed type.
You can use this type object to create a run-time instance of a constructed type.
Example
The following example uses the MakeGenericType method to create a generic type definition of a constructed type from the Dictionary<tkey, tvalue> type.
The constructed type represents the Dictionary<tkey, tvalue> object of the key of the Test string.
usingSystem;usingSystem.Reflection;usingSystem.Collections.Generic; Public classtest{ Public Static voidMain () {Console.WriteLine ("\ r \ n---Create a constructed type from the generic Dictionary type."); //Create A Type object representing the generic Dictionary//type, by omitting the type arguments (but keeping the//comma that separates them, so the compiler can infer the//Number of type parameters). Type generic =typeof(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 being contained in the Dictionary is Test.Type[] Typeargs = {typeof(string),typeof(Test)}; //Create a Type object representing the constructed generic//type.Type constructed =Generic. MakeGenericType (Typeargs); Displaytypeinfo (constructed); //Compare The type objects obtained above to type objects//obtained using typeof () and GetGenericTypeDefinition ().Console.WriteLine ("\ r \ 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 voidDisplaytypeinfo (Type t) {Console.WriteLine ("\r\n{0}", T); Console.WriteLine ("\tis This a generic type definition? {0}", t.isgenerictypedefinition); Console.WriteLine ("\tis it a generic type? {0}", T.isgenerictype); Type[] TypeArguments=t.getgenericarguments (); Console.WriteLine ("\tlist type arguments ({0}):", typearguments.length); foreach(Type Tparaminchtypearguments) {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 ' 2[tkey,tvalue] is this a Generic type definition? True is it a generic type? True List type arguments (2): TKey TValueSystem.Collections.Generic.Dictionary ' 2[syst Em. String, Test] is this a generic type definition? False is it a generic type? True List type arguments (2): System.String Test---Compare types obtained by differe NT Methods:are the constructed types equal? True is the generic types equal? True*/
Type.makegenerictype method (type[]) Generic reflection