13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

94 CHAPTER 3 Parameterized typ<strong>in</strong>g with genericsgeneric type def<strong>in</strong>itions—and there are new ones as well that let you <strong>in</strong>quire about thegeneric nature of the type.METHODS AND PROPERTIES OF SYSTEM.TYPEThere are far too many new methods and properties to look at them all <strong>in</strong> detail, butthere are two particularly important ones: GetGenericTypeDef<strong>in</strong>ition and Make-GenericType. They are effectively opposites—the first acts on a constructed type,retriev<strong>in</strong>g the generic type def<strong>in</strong>ition; the second acts on a generic type def<strong>in</strong>ition andreturns a constructed type. Arguably it would have been clearer if this method hadbeen called ConstructGenericType, MakeConstructedType, or some other name withconstruct or constructed <strong>in</strong> 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 call<strong>in</strong>gMakeGenericType twice with the same types as parameters will return the same referencetwice, and call<strong>in</strong>g GetGenericTypeDef<strong>in</strong>ition on two types constructed fromthe same generic type def<strong>in</strong>ition will likewise give the same result for both calls.Another method—this time one which already existed <strong>in</strong> .NET 1.1—that is worthexplor<strong>in</strong>g is Type.GetType, and its related Assembly.GetType method, both of whichprovide a dynamic equivalent to typeof. You might expect to be able to feed each l<strong>in</strong>eof the output of list<strong>in</strong>g 3.10 to the GetType method called on an appropriate assembly,but unfortunately life isn’t quite that straightforward. It’s f<strong>in</strong>e for closed constructedtypes—the type arguments just go <strong>in</strong> square brackets. For generic type def<strong>in</strong>itions,however, you need to remove the square brackets entirely—otherwise GetType th<strong>in</strong>ksyou mean an array type. List<strong>in</strong>g 3.11 shows all of these methods <strong>in</strong> action.List<strong>in</strong>g 3.11Various ways of retriev<strong>in</strong>g generic and constructed Type objectsstr<strong>in</strong>g listTypeName = "System.Collections.Generic.List`1";Type defByName = Type.GetType(listTypeName);Type closedByName = Type.GetType(listTypeName+"[System.Str<strong>in</strong>g]");Type closedByMethod = defByName.MakeGenericType(typeof(str<strong>in</strong>g));Type closedByTypeof = typeof(List);Console.WriteL<strong>in</strong>e (closedByMethod==closedByName);Console.WriteL<strong>in</strong>e (closedByName==closedByTypeof);Type defByTypeof = typeof(List);Type defByMethod = closedByName.GetGenericTypeDef<strong>in</strong>ition();Console.WriteL<strong>in</strong>e (defByMethod==defByName);Console.WriteL<strong>in</strong>e (defByName==defByTypeof);The output of list<strong>in</strong>g 3.11 is just True four times, validat<strong>in</strong>g that however you obta<strong>in</strong> areference to a particular type object, there is only one such object <strong>in</strong>volved.As I mentioned earlier, there are many new methods and properties on Type,such as GetGenericArguments, IsGenericTypeDef<strong>in</strong>ition, and IsGenericType. Thedocumentation for IsGenericType is probably the best start<strong>in</strong>g po<strong>in</strong>t for furtherexploration.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!