反射-如何设置值

时间:2020-10-16 22:45:09

标签: c# .net-core reflection system.reflection

Imageine一个rpg游戏。

我有一个角色。 我的角色在名为Equipment的课程中拥有装备插槽 这些插槽可以配备几种类型的物品:ArmorWeapon等)。 它们全都来自Item类。

每个Item都适合某个Equipment Slot。 每个Equipment Slot的类型为:

public enum EquipmentSlotType
{
    Head,
    LeftHand,
    RightHand,
    BothHands,
    Chest,
    Waist,
    Legs,
    Feet,
    Neck,
    LeftRingFinger,
    RightRingFinger,
}

我的Character课没那么有趣:

public class Character
{
    public string Name { get; private set; }
    public Equipment Equipment { get; private set; }
}

这是我的角色Equipment类:

public class Equipment
{
    public EquipmentSlot Head { get; private set; }
    public EquipmentSlot LeftHand{ get; private set; }
    public EquipmentSlot RightHand { get; private set; }
    public EquipmentSlot BothHands { get; private set; }
    public EquipmentSlot Chest { get; private set; }
    public EquipmentSlot Waist { get; private set; }
    public EquipmentSlot Legs { get; private set; }
    public EquipmentSlot Feet { get; private set; }
    public EquipmentSlot Neck { get; private set; }
    public EquipmentSlot LeftRingFinger { get; private set; }
    public EquipmentSlot RightRingFinger { get; private set; }
    private System.Reflection.PropertyInfo[] propertyInfos;
    public Equipment()
    {
        Head = new EquipmentSlot(EquipmentSlotType.Head, false);
        LeftHand = new EquipmentSlot(EquipmentSlotType.Head, false);
        RightHand = new EquipmentSlot(EquipmentSlotType.RightHand, false);
        BothHands = new EquipmentSlot(EquipmentSlotType.BothHands,false);
        Chest = new EquipmentSlot(EquipmentSlotType.Chest,false);
        Waist = new EquipmentSlot(EquipmentSlotType.Waist,false);
        Legs = new EquipmentSlot(EquipmentSlotType.Legs,false);
        Feet = new EquipmentSlot(EquipmentSlotType.Feet, false);
        Neck = new EquipmentSlot(EquipmentSlotType.Neck, false);
        LeftRingFinger = new EquipmentSlot(EquipmentSlotType.LeftRingFinger, false);
        RightRingFinger = new EquipmentSlot(EquipmentSlotType.RightRingFinger, false);
        
        propertyInfos = typeof(Equipment).GetProperties();
        
    }

}
    public bool EquipItemFromInventory(EquippableItem item, Character character)
    {
        
        // this needs fixing:
        var x = propertyInfos.First(pi => pi.Name.ToString() == item.EquipmentSlotType.ToString());
        var y = character.Equipment. .GetProperty(x.Name).SetValue(character, item.);

        
        
        if (item.EquipmentSlotType == EquipmentSlotType.BothHands)
        {
            LeftHand.IsUsed = true;
            RightHand.IsUsed = true;
            BothHands.IsUsed = true;
            BothHands.Item = item;
            return true;
        }
        else
        {
            switch (item.EquipmentSlotType)
            {
                case EquipmentSlotType.Head:
                    Head.IsUsed = true;
                    Head.Item = item;
                    return true;

                case EquipmentSlotType.Chest:
                    Chest.IsUsed = true;
                    Chest.Item = item;
                    return true;

                case EquipmentSlotType.LeftHand:
                    LeftHand.IsUsed = true;
                    LeftHand.Item = item;
                    return true;

                case EquipmentSlotType.RightHand:
                    RightHand.IsUsed = true;
                    RightHand.Item = item;
                    return true;

                case EquipmentSlotType.Waist:
                    Waist.IsUsed = true;
                    Waist.Item = item;
                    return true;

                case EquipmentSlotType.Legs:
                    Legs.IsUsed = true;
                    Legs.Item = item;
                    return true;

                case EquipmentSlotType.Neck:
                    Feet.IsUsed = true;
                    Feet.Item = item;
                    return true;
                
                case EquipmentSlotType.LeftRingFinger:
                    Feet.IsUsed = true;
                    Feet.Item = item;
                    return true;
                
                case EquipmentSlotType.RightRingFinger:
                    Feet.IsUsed = true;
                    Feet.Item = item;
                    return true;
                
                default:
                    return false;
            }
        }
    }
}       

我知道switch语句太大,应该重构。 我很确定我应该使用反射,但是我不知道该怎么做... 您可以在上方看到我的“结果”。

var x = propertyInfos.First(pi => pi.Name.ToString() == item.EquipmentSlotType.ToString());

这实际上似乎找到了正确的设备插槽。我传入了bow,它是从Weapon派生而来的,因此是从Item派生的,在调试过程中,我看到x的名称为BothHands,这是正确的。 bow具有EquipmentSlotType中的BothHands

因此,我认为我有适当的选择,但是我无法弄清楚将什么传递给.SetValue。 它需要我提供一个对象,但是我无法弄清楚哪一个:(

我在方法Character中添加了public bool EquipItemFromInventory(EquippableItem item, Character character),以便可以传递角色的实例。

有人可以告诉我如何使这种反射有效以及如何正确设置该值吗?

如果您需要更多信息,请告诉我。

非常感谢!!!

0 个答案:

没有答案