I am trying to cast from a child with a specific generic to a parent with a more general generic.
Take the following code:
public class AParent { }
public class AChild : AParent { }
public interface IParent<T>
{
public void func(T input);
}
public class Child : IParent<AChild>
{
public void func(AChild input) { }
}
public static void test()
{
IParent<AParent> parent = new Child();
}
In this code, I have AParent
and AChild
where AChild
inherits from AParent
.
I also have IParent
which takes in a generic type parameter and Child
which inherits from IParent
with a specific type of AChild
.
I feel like this should work logically? But I get the following error:
Cannot implicitly convert type 'Child' to 'IParent<AParent>'. An explicit conversion exists (are you missing a cast?)
I've also tried adding the in
/out
keyword to the T
type parameter on IParent
but to no avail. Is what I'm trying to do possible? It seems logical, what am I missing?