I have an Item and a subclass AdvancedItem (all made of value-types if that matters):
public Item
{
public string A;
public bool B;
public char C;
...// 20 fields
}
public AdvancedItem : Item
{
public string Z;
}
It's easy to simply create an Item or an AdvancedItem independently:
var item = new Item { A = "aa", B = true, C = 'c', ... };
var aItem = new AdvancedItem { A = "aa", B = true, C = 'c', ..., Z = "zz" };
Now, I just want to turn an Item into an AdvancedItem by providing it the string Z separately. In order to achieve that I was thinking of using a constructor.
Attempt A:
// annoying, we are not using the inheritance of AdvancedItem:Item
// so we will need to edit this whenever we change the class Item
public AdvancedItem(Item item, string z)
{
A = item.A;
B = item.B;
...;//many lines
Z = z;
}
Attempt B:
// to use inheritance it seems I need another constructor to duplicate itself
public Item(Item item)
{
A = item.A;
B = item.B;
...;//many lines
}
public AdvancedItem(Item item, string z) : base(Item)
{
Z = z;
}
Is there any way to improve this second attempt to avoid writing many lines of X = item.X
? Maybe a solution to auto-clone or auto-duplicate a class with itself where public Item(Item item)
would be wrote in one line?