C# 从变量获取字典值
我有一个字典,其中的值是一个类,因此我在字典中为每个键存储多个变量,例如-C# 从变量获取字典值,c#,class,dictionary,methods,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; 我想要的是- public void getMethod(Crew crews, var mod, List<Mo
public Dictionary<int, Crew> CrewList = new Dictionary<int, Crew>();
var crews = CrewList[x];
int hp = crews.HP;
int attack = crew.Attack;
我想要的是-
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;
}
}
}
}
public void getMethod(乘员组、var mod、列表modclass)
{
如果(crews.mod!=“”)
{
字符串mods=crews.mod;
foreach(modclass中的var mod)
{
if(mods==mod.Name)
{
strengthMod+=模态强度mod;
}
}
}
}
这是一个简化版本,但基本上是我想要做的。那么,有没有一种方法可以让crew.mod部分工作,用我试图返回的变量替换其中的'mod'部分呢?如果你想要的是
crew.Mod
而不是
crew["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; }
}
当然,如果您愿意,您可以向该界面添加更多统计信息。接下来,我将为您的项目创建一个项目类:
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 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);
此外,您可以用通用列表替换其中的一些属性。例如,Jon可能有双持剑的能力,在这种情况下,他可以携带两把剑。这是否与您所追求的类似:
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();
}
您必须使用反射,但我强烈建议您考虑将“修饰符”改为单独的集合(例如,代码>词典< /代码>)。然后您可以像
var mod=crew.Modifiers[“HP”]一样访问它代码>但该门槛会导致与crew.HP或crew.strengthMod相同的结果吗?我的示例稍微简化了一点,实际上每个修饰符都是一个字符串,然后我将通过Split(“:”)对其进行分隔。当调用getModifiers时,实际得到的是什么?特定机组的列表
?为什么这个方法不返回任何东西?我正在尝试根据我选择的mod和crew向公共int添加一个值。我可以为我需要的每一个修饰符单独编写一段代码,但我想如果这样做可行的话,它会更易于管理。这有点奇怪。你能告诉我们更多关于你试图解决的实际问题吗?谢谢,虽然我做了很多修改,但这确实有帮助。虽然没有真正回答我的主要问题,只是绕过了它。
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);
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);