从变量中获取字典值

本文关键字:字典 获取 变量 | 更新日期: 2023-09-27 18:12:58

我有一个字典,其中值是一个类,所以我在每个键的字典中存储多个变量,例如-

public Dictionary<int, Crew> CrewList = new Dictionary<int, Crew>();
var crews = CrewList[x];
int hp = crews.HP;
int attack = crew.Attack;

等等。我当前的代码是这样的

if (currentCrew.Weapon != "")
{
string weapons = currentCrew.Weapon;
   foreach (var weapone in weaponclass)
   {
       if (weapons == weapone.Name)
       {
            strengthMod += weapone.StrengthMod;
       }
   }
}

我想要的是-

public void getMethod(Crew crews, var mod, List<ModClasses> modclass)
{
if (crews.mod != "")
    {
    string mods = crews.mod;
       foreach (var mod in modclass)
       {
           if (mods == mod.Name)
           {
                strengthMod += mods.StrengthMod;
           }
       }
    }
}

这是一个简化的版本,但基本上是我想做的。那么有没有办法让船员。mod部分工作,它会用我试图返回的任何变量替换它的"mod"部分?

从变量中获取字典值

我觉得你的设计有点瑕疵。我是这样做的。首先,我创建了一个通用接口的任何类型的项目,有一个mod:

public interface IMod
{
    int HealthMod { get; set; }
    int ManaMod { get; set; }
    int StrengthMod { get; set; }
    int DexterityMod { get; set; }
    int ArmorMod { get; set; }
    string Name { get; set; }
}

当然,如果你愿意,你可以添加更多的统计数据到这个接口。接下来,我将为您的items创建一个item类:

public class ItemMod : IMod 
{
    // Implement Members
    public int HealthMod { get; set; }
    public int ManaMod { get; set; }
    public int StrengthMod { get; set; }
    public int DexterityMod { get; set; }
    public int ArmorMod { get; set; }
    public string Name { get; set; }
}

接下来,我将为船员创建一个接口来描述每个船员"拥有"的东西:

public interface ICrew
{
    IMod Weapon { get; set; }
    IMod Helm { get; set; }
    IMod Armor { get; set; }
}

所以这个界面基本上说所有的船员将有一个武器,一个头盔,和一件盔甲。显然,你可以添加更多。现在为您的船员类:

public class Crew : ICrew 
{
    // Implement ICrew members
    public IMod Weapon { get; set; }
    public IMod Helm { get; set; }
    public IMod Armor { get; set; }
    public string Name { get; set; }
    public int BaseHealth { get; set; }
    public int BaseMana { get; set; }
    public int BaseStrength { get; set; }
    public int BaseDexterity { get; set; }
    public int BaseArmor { get; set; }
    public int TotalHealth
    {
        get { return CalculateHealth(); }
    }
    public int TotalMana
    {
        get { return CalculateMana(); }
    }
    public int TotalStrength
    {
        get { return CalculateStrength(); }
    }
    public int TotalDexterity
    {
        get { return CalculateDexterity(); }
    }
    public int TotalArmor
    {
        get { return CalculateArmor(); }
    }
    private int CalculateHealth()
    {
        int additionalHealth = 0;
        if (Weapon != null)
            additionalHealth += Weapon.HealthMod;
        if (Helm != null)
            additionalHealth += Helm.HealthMod;
        if (Armor != null)
            additionalHealth += Armor.HealthMod;
        return additionalHealth + BaseHealth;
    }
    private int CalculateMana()
    {
        int additionalMana = 0;
        if (Weapon != null)
            additionalMana += Weapon.ManaMod;
        if (Helm != null)
            additionalMana += Helm.ManaMod;
        if (Armor != null)
            additionalMana += Armor.ManaMod;
        return additionalMana + BaseMana;
    }
    private int CalculateStrength()
    {
        int additionalStrength = 0;
        if (Weapon != null)
            additionalStrength += Weapon.StrengthMod;
        if (Helm != null)
            additionalStrength += Helm.StrengthMod;
        if (Armor != null)
            additionalStrength += Armor.StrengthMod;
        return additionalStrength + BaseStrength;
    }
    private int CalculateDexterity()
    {
        int additionalDexterity = 0;
        if (Weapon != null)
            additionalDexterity += Weapon.DexterityMod;
        if (Helm != null)
            additionalDexterity += Helm.DexterityMod;
        if (Armor != null)
            additionalDexterity += Armor.DexterityMod;
        return additionalDexterity + BaseDexterity;
    } 
    private int CalculateArmor()
    {
        int additionalArmor = 0;
        if (Weapon != null)
            additionalArmor += Weapon.ArmorMod;
        if (Helm != null)
            additionalArmor += Helm.ArmorMod;
        if (Armor != null)
            additionalArmor += Armor.ArmorMod;
        return additionalArmor + BaseArmor;
    }       
}

最后,下面是如何使用它:

var shortSword = new ItemMod();
shortSword.Name = "Short Sword of Darkness";
shortSword.ArmorMod = 15;
shortSword.HealthMod = 20;
var helm = new ItemMod();
helm.Name = "Glorious Helm of Madness";
helm.ArmorMod = 95;
helm.HealthMod = 82;
helm.DexterityMod = 12;
var breastPlate = new ItemMod();
breastPlate.ArmorMod = 145;
breastPlate.HealthMod = 33;
breastPlate.StrengthMod = 49;
var crew = new Crew();
crew.Name = "Jon";
crew.BaseHealth = 15;
crew.BaseMana = 9;
crew.BaseStrength = 25;
crew.BaseDexterity = 10;
crew.BaseArmor = 50;
crew.Weapon = shortSword;
crew.Helm = helm;
crew.Armor = breastPlate;
Console.WriteLine("{0} has {1} Health!", crew.Name, crew.TotalHealth);

此外,您可以用泛型列表替换其中的一些属性。例如,琼恩可能有双剑的能力,在这种情况下,他可以携带两把剑。

如果你想要的是

crew.Mod

代替

crew["Mod"]

你需要的是一个ExpandoObject

这是你想要的东西吗?

public int getMethod(
    Crew crews,
    Func<Crew, string> modName,
    Func<ModClasses, int> modValue,
    List<ModClasses> modclass)
{
    string mods = mod(crews);
    return mods == ""
        ? 0
        : modclass
            .Where(m => mods == m.Name)
            .Select(m => modValue(m))
            .Sum();
}

然后像这样调用它:

strengthMod += getMethod(crews, c => c.StrengthModName, mc => mc.StrengthMod, modclasses);
hpMod += getMethod(crews, c => c.HpModName, mc => mc.HpMod, modclasses);