Arguably it would have been clearer if this method had
been called ConstructGenericType, MakeConstructedType, or some other name with
construct or constructed in it, but we??™re stuck with what we??™ve got.
Just like normal types, there is only one Type object for any particular type??”so calling
MakeGenericType twice with the same types as parameters will return the same reference
twice, and calling GetGenericTypeDefinition on two types constructed from
the same generic type definition will likewise give the same result for both calls.
Another method??”this time one which already existed in .NET 1.1??”that is worth
exploring is Type.GetType, and its related Assembly.GetType method, both of which
provide a dynamic equivalent to typeof. You might expect to be able to feed each line
of the output of listing 3.10 to the GetType method called on an appropriate assembly,
but unfortunately life isn??™t quite that straightforward. It??™s fine for closed constructed
types??”the type arguments just go in square brackets. For generic type definitions,
however, you need to remove the square brackets entirely??”otherwise GetType thinks
you mean an array type. Listing 3.11 shows all of these methods in action.
string listTypeName = "System.Collections.Generic.List`1";
Type defByName = Type.GetType(listTypeName);
Type closedByName = Type.GetType(listTypeName+"[System.
Pages:
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205