C# 在列表中搜索子类属性

C# 在列表中搜索子类属性,c#,.net,C#,.net,现在我正在创建一个任务需求检查器,以确定玩家是否能够完成任务。我现在正在处理的任务类型是“库存中有一件物品”,如你所见,如果玩家的库存中有一件或多件指定物品,就可以完成这项任务 现在,我的确切问题是什么?嗯,这个。。。项目 首先Item是一个具有下一个结构的类: public int ID { get; set; } public string Name { get; set; } public double Price { get; set; } public Item(int id, st

现在我正在创建一个任务需求检查器,以确定玩家是否能够完成任务。我现在正在处理的任务类型是“库存中有一件物品”,如你所见,如果玩家的库存中有一件或多件指定物品,就可以完成这项任务

现在,我的确切问题是什么?嗯,这个。。。项目

首先
Item
是一个具有下一个结构的类:

public int ID { get; set; }
public string Name { get; set; }
public double Price { get; set; }

public Item(int id, string name, double price)
{
    ID = id;
    Name = name;
    Price = price;
}
还有一个名为
工具
的类,它扩展了
类:

public string Material { get; set; }
public string Classification { get; set; }

public Tool
    (
        int id,
        string name,
        double price,
        string material,
        string classification
    ) : base(id, name, price)
{
    Material = material;
    Classification = classification;
}
现在,我创建每个工具的方式如下:

Items.Tool tool = new Items.Tool(1, "Shovel", 100, "Wood", "Poor");
“我的玩家”对象包含以下项目和列表:

public List<Items.Item> InventoryItems { get; set; }
我所有的问题都出现在这里。这两行或代码是错误的:

x.Material == group.Key.Material &&
x.Classification == group.Key.Classification
因为在
项目中没有
物料
分类

我想做的是实施不同类型的评估

  • 如果一个任务需要一杯水,我应该在我的
    遗赠
    类中寻找居住的财产

  • 如果任务只需要一把剑。我应该在库存中寻找一件名为“剑”的物品

  • 如果任务需要一把钻石传奇剑,那么。。。你明白我的意思了

有没有办法在我的系统中查找这些扩展类属性?我找不到这样做的方法


PD:对不起,我的英语不好,不是母语。

编辑:我编辑了我的答案,以解决一般任务方法的问题

如果希望任务跨多个不同类型通用,可能需要对
Items.Item
实现
isame
IsEquivalent
方法,然后继承该方法。您甚至可以重写Object.Equals方法(这可能是一种更合适的方法)

然后在您的迭代中:

override public bool Accomplish()
{
    bool questAccomplished = true;
    foreach (var group in RequiredItems.GroupBy(x => x))
    {
        if (Application._player.InventoryItems.Count
            (
                x =>
                (
                   x.IsSame(group.Key)
                )
            ) < group.Count())
        {
            questAccomplished = false;
            break;
        }
    }

    return questAccomplished;
}
override public bool complete()
{
bool-questacomplethed=true;
foreach(requireItems.GroupBy中的var组(x=>x))
{
if(Application.\u player.InventoryItems.Count
(
x=>
(
x、 ISNAME(组密钥)
)
)
这将针对
工具进行调整
,除此之外什么都没有。如果这个任务要求玩家提供两种不同的物品,比如一把铁剑和一份遗赠,会发生什么?第一个将必须查找
项目。武器
名称
材料
,但第二个将查找
项目。Beberage
名称
。有没有办法创建某种if语句来检查任务所需物品中的当前物品类型(类型:工具、武器、遗赠等),以便应用不同的验证?我编辑了我的答案,以便更好地回答您的问题。我没有考虑一般的任务。完美的解决方案!你实际上教了我一些东西,这些东西将来会在很多其他方面帮助我。非常感谢你的帮助!
quest.AddRequiredItem(tool, 1);

public void AddRequiredItem(Items.Item item, int quantity)
{
    for(int i = 0; i < quantity; i++)
    {
        RequiredItems.Add(item);
    }
}
override public bool Accomplish()
{
    bool questAccomplished = true;
    foreach (var group in RequiredItems.GroupBy(x => x))
    {
        if (Application._player.InventoryItems.Count
            (
                x =>
                (
                    x.Name == group.Key.Name && 
                    x.Material == group.Key.Material &&
                    x.Classification == group.Key.Classification
                )
            ) < group.Count())
        {
            questAccomplished = false;
            break;
        }
    }

    return questAccomplished;
}
x.Material == group.Key.Material &&
x.Classification == group.Key.Classification
class Item
{
    public virtual bool IsSame(Item comp){ return comp.Name == Name; }
}

class Tool: Item
{
    public override bool IsSame(Item comp)
    {
        return base.IsSame(comp) && (comp is Tool) && ((Tool)comp).Material == Material && ((Tool)comp).Classification == Classification;
    }
}
override public bool Accomplish()
{
    bool questAccomplished = true;
    foreach (var group in RequiredItems.GroupBy(x => x))
    {
        if (Application._player.InventoryItems.Count
            (
                x =>
                (
                   x.IsSame(group.Key)
                )
            ) < group.Count())
        {
            questAccomplished = false;
            break;
        }
    }

    return questAccomplished;
}