Ok so I have an abstract base class called Product, a KitItem class that inherits Product and a PackageKitItem class that inherits KitItem. ie.
Product
KitItem : Product
PackageKitItem : KitItem
I have my KitItems loaded and I need to load up a collection of PackageKitItems which are, effectively, shallow copies of KitItems.
Currently we are doing what feels to me a hacky shallow copy in the Product constructor like so:
public Product(Product product)
{
FieldInfo[] fields = product.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
// copy each value over to 'this'
foreach (FieldInfo fi in fields)
fi.SetValue(this, fi.GetValue(product));
}
I've tried setting up a copy on KitItem like so:
public KitItem ShallowCopy()
{
return (KitItem)this.MemberwiseClone();
}
and calling it thus:
PackageKitItem tempPackKitItem = (PackageKitItem)packKitItem.ShallowCopy();
but I get an invalid cast. I'm looking for ideas for the best way to accomplish this.