Possible Duplicate:
How to use reflection to call generic Method?
I have a method with the below signature
public string Register<T>()
{
//code
}
which can be invoked like this
var result = Register<Employee>();
my requirement is to invoke the method by reading the type T from config file,
ie:
While invoking, instead of hardcoding “Employee”, i should be able to supply it dynamically by reading the config file.
Any idea on how to do it?
Well you can use reflection, specifically MakeGenericMethod, but if you don’t have the type at compile time it seems better to change the method signature to
Register(Type type). Then you can read the type out of the config file and pass it in directly.Edit
If you add a reference to
SimpleInjector.Extensions, you will be able to access non-generic registration methods. See NonGenericRegistrationsExtensions.cs in the source here. This will allow you to do something like this:Then you don’t need generics at all.
You might also want to read over Advanced Scenarios