I need to keep several children of a base class in a list. Normally not an issue, but since Generics are involved this became a head-scratcher for me. My base class looks like this:
public abstract class Equipment<T> where T:EquipmentData { }
Now a child may look like:
public class Weapon : Equipment<WeaponData> { }
with
public class WeaponData : EquipmentData { }
Analogous there's also:
public class Shield : Equipment<ShieldData> { }
public class ShieldData : EquipmentData { }
Now I need a list that contains children of Equipment<T>
but apparently List<T>
requires me to be more specific, like List<Equipment<EquipmentData>>
. However when I try and add a Weapon
object to that list, I get the error that it's not possible to convert Weapon
into Equipment<EquipmentData>
. Of course changing the list to List<Equipment<WeaponData>>
will work, but then I won't be able to add Shield
objects to that list for the same (obvious) reason.
In essence my question is if there's some way to make the List a
List<Equipment<
child ofEquipmentData
>> so that it would take Weapon
objects as well as Shield
objects?