I can see that you are hoping that there may be classes derived from Type
e.g. class WindowType : Type {} which could be used to provide some static type safety for your method but unfortunately there are not.
... This is not entirely true.
Type
is abstract - the compiler (or code generator) creates a implementation (a derivate of
Type
for each new type (
class
,
struct
,
enum
,...) defined by the program. These derivates are not explicitely accessible by the application, similar to anonymous types or types of anonymous delegates or lambdas.
You can enforce the compile type safety you require as follows:
private void CreateSomething<AllowedType>() where AllowedType : System.Windows.Window
{
}
The client needs to define type for which the operation should be performed:
CreateSomething<System.Windows.Window>();
CreateSomething<System.Windows.NavigationWindow>();
CreateSomething<System.String>();
Observe that you don't need to invoke
GetType
or
typeof
at all since the compiler does not even let you get that far.