No , that is impossible. FrameworkElement.GetType()and are FooTypenot related to the type system, both are just Types. If you want to limit your generic type, you will have to do this with runtime checks and exceptions; general constraints will not help you.
If you know what you want to store at compile time (or are happy with some kind of complex reflection), you can change your API to not accept Type, but use the generic code instead:
public void AddTypeForType(Type x, Type y)
can be replaced by
public void AddTypeForType<T1, T2>() where T1 : FrameworkElement
{
myDict.Add(typeof(T1), typeof(T2));
}
Then you can call it like this:
AddTypeForType<FrameworkDerivedClass, MyCustomClass>();
But this is more of an API change than the answer to your question.
source
share