Casting class with generic variable to the same class with an interface that the generic implements
i have a variable of type ScriptableUnit<MinionStats> and im trying to cast it to ScriptableUnit<IUnitStats> and it does not work even though MinionStats implements the IUnitStats interface, do you know why and how i can deal with it?
2 Replies
What is a ScriptableUnit? But this generally is a problem related to variance, see https://learn.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance
Covariance and Contravariance in Generics - .NET
Learn about covariance, which allows you to use a more derived type, and contravariance, which allows you to use a less derived type, in .NET generics.
ScriptableUnit
has to be an interface with an out
generic argument
You can't do that with a class