First, look at the syntax for obtaining the generic type definition of Dictionary
. The comma in the angle brackets is required to effectively tell the compiler
to look for the type with two type parameters: remember that there can be several
generic types with the same name, as long as they vary by the number of type parameters
they have. Similarly, you??™d retrieve the generic type definition for MyClass
using typeof(MyClass<,,,>). The number of type parameters is specified in IL (and in
full type names as far as the framework is concerned) by putting a back tick after the first
part of the type name and then the number. The type parameters are then indicated in
square brackets instead of the angle brackets we??™re used to. For instance, the second line
printed ends with List`1[T], showing that there is one type parameter, and the third
line includes Dictionary`2[TKey,TValue].
Second, note that wherever the method??™s type parameter is used, the actual value
of the type argument is used at execution time. So the first line B prints List`1
rather than List`1, which you might have expected. In other
words, a type that is open at compile time may be closed at execution time. This is very
confusing. You should be aware of it in case you don??™t get the results you expect, but otherwise
don??™t worry. To retrieve a truly open constructed type at execution time, you need to
work a bit harder.
Pages:
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203