简体   繁体   English

C#,如何将派生列表 class 传递给接收基列表 class 的方法?

[英]C#, How to pass a List of a Derived class to a method that receives a List of the Base class?

This is a simplified version of my code:这是我的代码的简化版本:

using System.Collections.Generic;

public abstract class FruitBox<T>
{
    public T item;

    public static T ChooseFirst(List<FruitBox<T>> fruitBoxes)
    {
        return fruitBoxes[0].item;
    }
}

public class Apple
{
}

public class AppleBox : FruitBox<Apple>
{
}

public class FruitShop
{
    List<AppleBox> appleBoxes = new List<AppleBox>();

    public void Main()
    {
        AppleBox appleBox = new AppleBox();
        appleBoxes.Add(appleBox);

        AppleBox.ChooseFirst(appleBoxes); // => Error here
    }
}

I have an error in the line:我在该行中有一个错误:

AppleBox.ChooseFirst(appleBoxes);

cannot convert from System.Collections.Generic.List<AppleBox> to System.Collections.Generic.List<FruitBox<Apple>>无法从System.Collections.Generic.List<AppleBox>转换为System.Collections.Generic.List<FruitBox<Apple>>

I tried:我试过了:

AppleBox.ChooseFirst((List<FruitBox<Apple>>)appleBoxes);

But same error.但是同样的错误。

How do I have to proceed?我该如何进行?

The reason for such behaviour is explained here .这种行为的原因在这里解释。 In short - classes do not support variance in C# and List<AppleBox> is not List<FruitBox<Apple>> .简而言之 - 类不支持C#中的差异并且List<AppleBox>不是List<FruitBox<Apple>>

What you can do:你可以做什么:

  • "convert" collection (actually create a new one): “转换”集合(实际上是创建一个新集合):

with OfType<>().ToList()使用OfType<>().ToList()

AppleBox.ChooseFirst(appleBoxes.OfType<FruitBox<Apple>>().ToList())

or just ToList或者只是ToList

AppleBox.ChooseFirst(appleBoxes.ToList<FruitBox<Apple>>())
public abstract class FruitBox<T>
{
    public T item;

    public static T ChooseFirst(IEnumerable<FruitBox<T>> fruitBoxes)
    {
        return fruitBoxes.First().item;
    }
}

You will have to hold the reference of the derived class into the base class variable您必须将派生 class 的引用保存到基础 class 变量中

List<FruitBox<Apple>> appleBoxes = new List<AppleBox>();

FruitBox<Apple> appleBox = new AppleBox();
appleBoxes.Add(appleBox);

appleBox.ChooseFirst(appleBoxes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将基类发送到接收派生类的方法-C# - Sending base class to a method which receives derived class - c# 您能否将派生类添加到其基类列表中,然后从C#中的基类列表中调用派生类的方法 - Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C# C# - 在基本 class 列表中序列化派生 class 的所有属性 - C# - Serialize all attributes of derived class in a list of base class c#如何通过反射获取派生类属性列表,先按基类属性排序,然后派生类props - c# how to get list of derived class properties by reflection, ordered by base class properties first, and then derived class props c#派生类列表 - c# List of derived class C#创建一个List以保存基类的任何派生子级 - C# Make a List to hold any derived children of a base class 如何将派生类传递给基类函数并返回派生类C# - How to pass a derived class to a base class function and return the derived class c# 在C#中如何从派生类实例调用已经在派生类中覆盖的基类方法 - in C# how to invoke base class method, which already override in derived class, from derived class instance 如何从 C# 中的派生类实例调用基方法? - How to call base method from derived class instance in C#? c#泛型基类方法返回派生类的类型 - c# generic base class method return the type of the derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM