Generics and Reflection via TDD

Pluggable development requires trickery. Sometimes naivety is a requirement in the development of pluggable solutions and, though not always the best idea, dynamically-dynamic code is the only way these problems can be solved. If for no other reason than to have a record of how to solve certain situations when they arise in my own life again, I’m going to try to put together a series of posts on how reflection and generic usage can be pretty neat together.


This first post is to answer a question from a colleague tonight. He’s reading a type via a custom configuration element and then using that type as a generic parameter to a method call.

How do I call a generic method if I don’t know the type I’ll be providing the generic argument at run-time?

The question’s one I’ve had to remember how to do a few times, so here’s to hoping it helps someone else. The screen shot below contains a unit test class with a sample generic method. The test points to a type’s generic method using reflection via the MethodInfo.MakeGenericMethod method and then calls the method using the MethodInfo.Invoke method.

Happy coding!

Brady Gaster
Hi! I'm a christian dad who lives near Seattle, where I work with the talented folks at Microsoft to create compelling demonstrations for conferences that instruct and inspire developers who want to party in the cloud.