I am creating a game and am currently working on an Inventory system for it. I have a class to represent a player that looks like this:
class Player {
public Weapon WeaponSlot {get;set;}
public Shield ShieldSlot {get;set;}
}
Weapons, Shield, etc. are subclasses of the general class Item:
class Item {
}
class Weapon : Item {
}
There are subclasses of weapons, etc., but that doesn't matter.
In any case, I create a UserControl to display / modify the contents of this inventory slot. However, I do not know exactly how to do this. In C ++, I would use something like:
new InventorySlot(&(player.WeaponSlot));
But I can not do this in C #.
I found the TypedReference structure, but this does not work, since you are not allowed to create a field with one of these structures, so I could not store it for use later in the control.
Is reflection the only way, or is there another means that I don't know about?
EDIT ----
, :
partial class InventorySlot : UserControl {
PropertyInfo slot;
object target;
public InventorySlot(object target, string field) {
slot = target.GetType().GetProperty(field);
if (!slot.PropertyType.IsSubclassOf(typeof(Item)) && !slot.PropertyType.Equals(typeof(Item))) throw new
this.target = target;
InitializeComponent();
}
}
:
new InventorySlot(player, "WeaponSlot");
, , . , .:)