从变量中获取字典值

时间:2014-07-14 21:14:09

标签: c# class dictionary methods

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

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;
           }
       }
    }
}

这是一个简化版本,但基本上我想做的。那么有没有办法让crew.mod部分工作,它将取代&#39; mod&#39;我试图返回的任何变量的一部分?

3 个答案:

答案 0 :(得分:1)

我认为你的设计有点瑕疵。我就是这样做的。首先,我为任何具有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; }
}

当然,如果您愿意,可以在此界面添加更多统计数据。接下来,我要为您的商品创建商品类:

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; }
}

接下来,我为船员创建一个界面来描述每个船员&#34;&#34;:

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);

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

答案 1 :(得分:0)

如果您追求的是

crew.Mod

而不是

crew["Mod"]

您需要的是ExpandoObject

答案 2 :(得分:0)

这类似于你之后的事情:

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);