13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Advanced generics93List<strong>in</strong>g 3.10Us<strong>in</strong>g the typeof operator with type parametersstatic void DemonstrateTypeof(){Console.WriteL<strong>in</strong>e(typeof(X));Console.WriteL<strong>in</strong>e(typeof(List));Console.WriteL<strong>in</strong>e(typeof(Dictionary));Displays method’stype parameterConsole.WriteL<strong>in</strong>e(typeof(List));Console.WriteL<strong>in</strong>e(typeof(Dictionary));DisplaysgenerictypesConsole.WriteL<strong>in</strong>e(typeof(List));Console.WriteL<strong>in</strong>e(typeof(Dictionary));}...DemonstrateTypeof();Displays closedtypes (despiteus<strong>in</strong>g typeparameter)Most of list<strong>in</strong>g 3.10 is as you might naturally expect, but it’s worth po<strong>in</strong>t<strong>in</strong>g out twoth<strong>in</strong>gs. First, look at the syntax for obta<strong>in</strong><strong>in</strong>g the generic type def<strong>in</strong>ition of Dictionary. The comma <strong>in</strong> the angle brackets is required to effectively tell the compilerto look for the type with two type parameters: remember that there can be severalgeneric types with the same name, as long as they vary by the number of type parametersthey have. Similarly, you’d retrieve the generic type def<strong>in</strong>ition for MyClassus<strong>in</strong>g typeof(MyClass). The number of type parameters is specified <strong>in</strong> IL (and <strong>in</strong>full type names as far as the framework is concerned) by putt<strong>in</strong>g a back tick after the firstpart of the type name and then the number. The type parameters are then <strong>in</strong>dicated <strong>in</strong>square brackets <strong>in</strong>stead of the angle brackets we’re used to. For <strong>in</strong>stance, the second l<strong>in</strong>epr<strong>in</strong>ted ends with List`1[T], show<strong>in</strong>g that there is one type parameter, and the thirdl<strong>in</strong>e <strong>in</strong>cludes Dictionary`2[TKey,TValue].Second, note that wherever the method’s type parameter is used, the actual valueof the type argument is used at execution time. So the first l<strong>in</strong>e B pr<strong>in</strong>ts List`1 rather than List`1, which you might have expected. In otherwords, a type that is open at compile time may be closed at execution time. This is veryconfus<strong>in</strong>g. You should be aware of it <strong>in</strong> case you don’t get the results you expect, but otherwisedon’t worry. To retrieve a truly open constructed type at execution time, you need towork a bit harder. See the MSDN documentation for Type.IsGenericType for a suitablyconvoluted example.For reference, here’s the output of list<strong>in</strong>g 3.10:System.Int32System.Collections.Generic.List`1[T]System.Collections.Generic.Dictionary`2[TKey,TValue]System.Collections.Generic.List`1[System.Int32]System.Collections.Generic.Dictionary`2[System.Str<strong>in</strong>g,System.Int32]System.Collections.Generic.List`1[System.Int64]System.Collections.Generic.Dictionary`2[System.Int64,System.Guid]Hav<strong>in</strong>g retrieved an object represent<strong>in</strong>g a generic type, there are many “next steps”you can take. All the previously available ones (f<strong>in</strong>d<strong>in</strong>g the members of the type, creat<strong>in</strong>gan <strong>in</strong>stance, and so on) are still present—although some are not applicable forBDisplaysclosed typesLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!