Reputation: 1318
I have the following static class
public static class MyFoo<T> where T : class
{
public static IBar<T> Begin()
{
return new Bar<T>();
}
}
Elsewhere, I have a variable of System.Type, which I need to pass in as the Generic Type argument, ideally:
public void MyFunction(Type type)
{
MyFoo<type>.Begin();
}
I know its possible through reflection to return an instance of MyFoo (If i make it non static) but this would not be ideal. Is there anyway to pass type as a Generic Type argument to the MyFoo static class?
Upvotes: 3
Views: 1611
Reputation: 416149
Can't do it. The value of the type
variable in the second sample is only known at run time. The T
type parameter in the first sample must be resolved at compile time.
Instead, look into Activator.CreateInstance()
.
Upvotes: 4