I have two classes (this is C#) that are very similar except they each contain their own nested class and enum.
I would like to refactor them to both inherit from a single abstract class, but I’m running into a problem because the methods are all tightly coupled to the nested class types.
My first plan was to pull out the ItemDetails Class, but it is linked to the ItemType, which is an enum that is specific to each view item class. Further, I can’t just use System.Enum as the type since I need to be able to serialize the details to an xml file.
How could I reduce the duplication within these classes?
public class FirstViewItem
{
[Serializable]
public class ItemDetails
{
public ItemType Type;
public int Width;
public string Text;
public int DisplayOrder;
}
public enum ItemType
{
None = 0,
A,
B,
C
}
public FirstViewItem()
{
// ...
}
public List<ItemDetails>()
{
// code here ...
}
}
public class SecondViewItem
{
[Serializable]
public class ItemDetails
{
public ItemType Type;
public int Width;
public string Text;
public int DisplayOrder;
}
public enum ItemType
{
None = 0,
X,
Y,
X
}
public SecondViewItem()
{
// ...
}
public List<ItemDetails>()
{
// code here ...
}
}
You want to make a generic class that is dependent on the item type enum being passed in:
Then some item types:
Then usage: