目标:从字典中获取一个值。所述值具有字典作为键。
我在做什么:我正在创建第二个字典,它的值与我试图获取的键的值完全相同。使用TryGetValue
结果:期望一个值但得到空值;
背景: 我正在尝试在 Unity 中制作制作功能。这就是工艺成分类的样子(ICombinable 现在看起来完全一样):
public class Ingredient : ICombinable
{
public string Name { get; set; }
public string Description { get; set; }
public Effect Effect { get; set; }
}
在实践中,我希望用户能够将 ICombinable 类型的对象拖到 UI(未实现)上并按下按钮将它们组合成一个新项目。例如,2 种药草和 1 杯水返回治疗药水(一种新物品)。
在表面后面,我会将拖动/选择的对象存储Dictionary<ICombinable, int>
在 int 是 per 的数量ICombinable
。
在另一个类中,我正在存储另一个字典,它将保存所有的食谱。
public class Combiner
{
private Dictionary<Dictionary<ICombinable, int>, ICraftable> _recipebook;
public Combiner()
{
_recipebook = new Dictionary<Dictionary<ICombinable, int>, ICraftable>(); // <Recipe, Item it unlocks>
}
public void AddRecipe(Dictionary<ICombinable, int> recipe, ICraftable item) => _recipebook.Add(recipe, item);
public ICraftable Craft(Dictionary<ICombinable, int> ingredientsAndAmount) =>
_recipebook.TryGetValue(ingredientsAndAmount, out var item) == false ? null : item;
//FirstOrDefault(x => x.Key.RequiredComponents.Equals(givenIngredients)).Value;
}
_recipebook 的关键是由成分及其数量组成的实际配方。ICraftable 是与该配方对应的对象/项目。在我之前给出的示例中,ICraftable 是治疗药水,两根木棍和一杯水都是字典中的一个条目,它是该值的关键。
最后,Craft 方法需要一个字典(换句话说,一个成分列表及其数量),我希望它在 _recipebook 中检查与给定字典对应的项目。如果成分组合有效,则应返回一个项目,否则为 null。
我如何测试这个功能: 我刚开始这个项目,所以我想从单元测试开始。这是设置:
[Test]
public void combiner_should_return_healing_potion()
{
// Use the Assert class to test conditions
var combiner = new Combiner();
var item = new Item
{
Name = "Healing Potion",
Unlocked = false
};
combiner.AddRecipe(new Dictionary<ICombinable, int>
{
{new Ingredient {Name = "Herb", Description = "Has healing properties", Effect = Effect.Heal}, 3},
{new Ingredient {Name = "Water", Description = "Spring water", Effect = default}, 1},
{new Ingredient {Name = "Sugar", Description = "Sweetens", Effect = default}, 2}
},
item);
var actualItem = combiner.Craft(new Dictionary<ICombinable, int>
{
{new Ingredient { Name = "Herb", Description = "Has healing properties", Effect = Effect.Heal} , 3},
{new Ingredient {Name = "Water", Description = "Spring water", Effect = default}, 1},
{new Ingredient {Name = "Sugar", Description = "Sweetens", Effect = default}, 2}
});
Assert.That(actualItem, Is.EqualTo(item));
}
结果:
combiner_should_return_healing_potion (0.023s)
---
Expected: <Models.Item>
But was: null
---
我正在创建一个名为治疗药水的项目和一本应该是它的食谱的字典。我将这些添加到食谱书中。之后,我正在创建第二个字典来“模拟”用户的输入。该词典的内容与我使用 Add recipe() 添加到食谱书中的内容完全相同。为什么TryGetValue
不认为这两个字典是平等的?
我该怎么做才能让它工作?