public static void Main()
ContractItemInventory item = new ContractItemInventory();
var a = item.Cast<ContractItemWeapon>();
Console.WriteLine(a.SlotNumber);
public class ContractItemInventory<CItem> where CItem : ContractItem
public string CharInventoryIDRef { get; set; }
public CItem Item { get; set; }
public int SlotNumber { get; set; }
public ContractItemInventory<T> Cast<T>() where T : ContractItem
if (typeof(CItem).IsSubclassOf(typeof(T)))
Console.WriteLine("boo");
return this as ContractItemInventory<T>;
return new ContractItemInventory<T>()
CharInventoryIDRef = this.CharInventoryIDRef,
SlotNumber = this.SlotNumber
public T CastNew<T, U>() where T : ContractItemInventory<U> where U : ContractItem
public class ContractItemInventory : ContractItemInventory<ContractItem> { }
public class ContractItemEquippableInventory : ContractItemInventory<ContractItemEquippable> { }
public class ContractItemWeaponInventory : ContractItemInventory<ContractItemWeapon> { }
public class ContractItemArmorInventory : ContractItemInventory<ContractItemArmor> { }
public class ContractItem{}
public class ContractItemEquippable: ContractItem{}
public class ContractItemWeapon: ContractItemEquippable{}
public class ContractItemArmor: ContractItemEquippable{}