将子类型对象添加到超类型列表中,然后从超类型列表中返回子类型列表

x按钮不起作用

我有3个接口。

public interface IItem
{
    string Name { get; set; }
}

public interface IEquipable : IItem
{
    void Equip();
}

public interface IConsumable : IItem
{
    void Use();
}

IEquipable通过类来实现Helmet,并BowIConsumable由类实现PotionFood

然后,我有一个属性包含一个 List of 的类IItemIEquipableIConsumable在实例化它之后继续添加两个项目

public class Character
{
    public List<IItem> Items { get; private set; }

    public Character()
    {
        this.Items = new List<IItem>();
    }

    public void AddItem(IItem item)
    {
        this.Items.Add(item);
    }
}

程序.cs

...
Character char = new Character();
char.AddItem(new Potion());
char.AddItem(new Food());
char.AddItem(new Helmet());
char.AddItem(new Bow());
...

有没有办法可以IEquipableIItem每个 AS的列表中获取所有成员的列表IEquipable

我想做类似的事情

    ...
    List<IEquipable> equipmentList = //do something to char.Items and get all items of type IEquipable.
    IEquipment equipment = equipmentList.First(...)
    equipment.Equip();
    ...

我试过使用,List<IEquipable> equipmentList = char.Items.OfType<IEquipable>().ToList()但结果列表最终为空。

神秘性

我实现(并修复了小错别字)你的代码是这样的:

void Main()
{
    Character character = new Character();
    character.AddItem(new Potion());
    character.AddItem(new Food());
    character.AddItem(new Helmet());
    character.AddItem(new Bow());
    
    List<IEquipable> equipmentList = character.Items.OfType<IEquipable>().ToList();
}

public class Potion : IConsumable
{
    public string Name { get; set; }

    public void Use()
    {
        throw new NotImplementedException();
    }
}

public class Food : IConsumable
{
    public string Name { get; set; }

    public void Use()
    {
        throw new NotImplementedException();
    }
}

public class Helmet : IEquipable
{
    public string Name { get; set; }

    public void Equip()
    {
        throw new NotImplementedException();
    }
}

public class Bow : IEquipable
{
    public string Name { get; set; }

    public void Equip()
    {
        throw new NotImplementedException();
    }
}

public interface IItem
{
    string Name { get; set; }
}

public interface IEquipable : IItem
{
    void Equip();
}

public interface IConsumable : IItem
{
    void Use();
}

public class Character
{
    public List<IItem> Items { get; private set; }

    public Character()
    {
        this.Items = new List<IItem>();
    }

    public void AddItem(IItem item)
    {
        this.Items.Add(item);
    }
}

您的确切代码(尽管已char重命名为character)运行良好。equipmentList两个元素结束。您看到的问题,即“结果列表最终为空”,无法使用您发布的代码重现。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Kotlin中将子类型列表添加到超类型列表中

从超类型的扩展列表返回子类型对象的列表

尝试将超类对象添加到列表时如何克服列表子类型推断问题?

将子类型添加到通用列表

在子类中定义列表的类型

您如何将超类型列表转换为子类型列表?

将项目添加到列表对象 c# 中的属性(列表对象类型)

列表子类的 Python 类型

在Dart中获取通用列表的子类型

将枚举类型添加到列表

Dart:超类型列表仅在运行时采用子类型

从子类型到超类型的ClassCastException(在Scala中)

返回子类作为超类的类型

C#-子类类型列表

将类添加到WordPress仪表板上的自定义帖子类型列表页面

超类类型对象的子类的构造方法?

Swift:从超类返回子类类型的对象数组

链接子类型中类型参数的注释以声明超类型中的类型参数

如何从 Python 中现有的超类类型对象实例化子类类型变量

如何基于超类型的对象创建子类型的对象?

子类型与其超类型的交集是否与子类型相同?

无法将子类型列表传递到接口类型列表(由子类型实现)到 Visual Basic 中的函数 + 也不能强制转换

不管对象是什么类型,都将其添加到方法中的列表中

在 wpf C# 中从对象类型 Json 数据添加到列表中

根据对象类型将对象添加到列表

自定义帖子类型中的Wordpress列表类别

yojson 列表中 Yojson 元素的子类型化

如何将泛型类型的实例添加到 Kotlin 中的列表中

将子类型的集合转换为超类型的集合