Is it possible to do something like this?
Type MyType = typeof(SomeClass);
SomeMethod<MyType>();
Where SomeMethod
is,
T SomeMethod<T>() where T : TypeOfSomeClass {
...
return NewInstanceOfT;
}
This fails, of course. I want to store the type of a class and later use that type (the value of MyType
) as the generic type for a method.
This is an use case as an example:
class WindowService {
public static T CreateWindow<T>() where T : Window {
return (T)Activator.CreateInstance(typeof(T));
}
}
... (in some other class) ...
Type TypeOfWindow = typeof(MyConcreteWindow);
void CreateAWindow() {
Window Obj = WindowService.CreateWindow<TypeOfWindow>();
}
CreateWindow<T>
should create an instance of whatever class is stored in TypeOfWindow
, as long as it extends Window
.