Lets say, If I have a situation like the following.
Type somethingType = b.GetType();
// b is an instance of Bar();
Foo<somethingType>(); //Compilation error!!
//I don't know what is the Type of "something" at compile time to call
//like Foo<Bar>();
//Where:
public void Foo<T>()
{
//impl
}
How should I call the generic function without knowing the type at compile time?
You’ll need to use reflection:
When writing a generic method, it’s good practice to provide a non-generic overload where possible. For instance, if the author of
Foo<T>()had added aFoo(Type type)overload, you wouldn’t need to use reflection here.