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.

Advanced generics95REFLECTING GENERIC METHODSGeneric methods have a similar (though smaller) set of additional properties andmethods. List<strong>in</strong>g 3.12 gives a brief demonstration of this, call<strong>in</strong>g a generic method byreflection.List<strong>in</strong>g 3.12Retriev<strong>in</strong>g and <strong>in</strong>vok<strong>in</strong>g a generic method with reflectionpublic static void Pr<strong>in</strong>tTypeParameter(){Console.WriteL<strong>in</strong>e (typeof(T));}...Type type = typeof(Snippet);MethodInfo def<strong>in</strong>ition = type.GetMethod("Pr<strong>in</strong>tTypeParameter");MethodInfo constructed;constructed = def<strong>in</strong>ition.MakeGenericMethod(typeof(str<strong>in</strong>g));constructed.Invoke(null, null);First we retrieve the generic method def<strong>in</strong>ition, and then we make a constructedgeneric method us<strong>in</strong>g MakeGenericMethod. As with types, we could go the other way ifwe wanted to—but unlike Type.GetType, there is no way of specify<strong>in</strong>g a constructedmethod <strong>in</strong> the GetMethod call. The framework also has a problem if there are methodsthat are overloaded purely by number of type parameters—there are no methods<strong>in</strong> Type that allow you to specify the number of type parameters, so <strong>in</strong>stead you’d haveto call Type.GetMethods and f<strong>in</strong>d the right one by look<strong>in</strong>g through all the methods.After retriev<strong>in</strong>g the constructed method, we <strong>in</strong>voke it. The arguments <strong>in</strong> this exampleare both null as we’re <strong>in</strong>vok<strong>in</strong>g a static method that doesn’t take any “normal”parameters. The output is System.Str<strong>in</strong>g, as we’d expect.Note that the methods retrieved from generic type def<strong>in</strong>itions cannot be <strong>in</strong>vokeddirectly—<strong>in</strong>stead, you must get the method from a constructed type. This applies toboth generic methods and nongeneric methods.Aga<strong>in</strong>, more methods and properties are available on MethodInfo, and IsGeneric-Method is a good start<strong>in</strong>g po<strong>in</strong>t <strong>in</strong> MSDN. Hopefully the <strong>in</strong>formation <strong>in</strong> this section willhave been enough to get you go<strong>in</strong>g, though—and to po<strong>in</strong>t out some of the added complexitiesyou might not have otherwise anticipated when first start<strong>in</strong>g to access generictypes and methods with reflection.That’s all we’re go<strong>in</strong>g to cover <strong>in</strong> the way of advanced features. Just to reiterate,this is not meant to have been an absolutely complete guide by any means—but mostdevelopers are unlikely to need to know the more obscure areas. I hope for your sakethat you fall <strong>in</strong>to this camp, as specifications tend to get harder to read the deeper yougo <strong>in</strong>to them. Remember that unless you’re work<strong>in</strong>g alone and just for yourself,you’re unlikely to be the only one to work on your code. If you need features that aremore complex than the ones demonstrated here, you almost certa<strong>in</strong>ly shouldn’tassume that anyone read<strong>in</strong>g your code will understand it without help. On the otherhand, if you f<strong>in</strong>d that your coworkers don’t know about some of the topics we’ve coveredso far, please feel free to direct them to the nearest bookshop…Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!