If you find yourself writing a lot of the same code (or copy/pasting it) for similar constructs, you should probably consider using generics. It allows you to write a base class full of functionality that is basically the same across all of different classes, but in a way that doesn’t tie you to a specific object model. Coupled with abstract and virtual methods, you can, in some cases, create the vast majority of functionality generically.
Reflection
But what if you want to access a method or property from a generic class or method? Say you have a list of types that you know are used by a generic base type, so now you want to write some unit tests to test the basic functionality or to simply make sure that an implementation exists representing that type?
MakeGenericMethod
Luckily, there’s MakeGenericMethod. This will create a MethodInfo object with the specified type as its generic type parameter. Then you can invoke it like you normally would:
MakeGenericType
Similarly, if your type relies on a generic (think List<T>), you can make a generic type to use with reflection.
You can do a typeof(MyClass<>) without anything in between the angle brackets and then call MakeGenericType to create the fully qualified type.
Getting Nested Classes With Reflection – Darchuk.NET
[…] talked about reflection a couple of times before. It allows you to dive deep into a type and get out […]