Asked question: how to implement a common list List<Item>return elements with the Data property, but return different types for different subclasses? I began to create the following hierarchy, but this does not lead to the goal.
abstract class Item
abstract class ItemGeneric<TData> : Item
class ItemText : ItemGeneric<string>
class ItemImage : ItemGeneric<Image>
I create a bunch of instances of the ItemText and ItemImage classes and add them to the general List<Item>list. However, as soon as I look through the list and want to get the Data property, it is not available (obviously), it was implemented only at the level of the class hierarchy ItemGeneric<TData>, and not in the Item class.
I would like to solve this problem without using System.Objectto avoid casting.
Is there a common template to solve this problem?