1

我有两个小类,每个类Pet都有Book几个属性和一个带有类似方法的函数

 public static List<T> GetSubSet<T>(List<T> incomingList)
        {
            var returnList = new List<T>();
            Random r = new Random();
            Console.WriteLine("Enter size of random subset: ");
            int randomInt = 0;
            int size = Convert.ToInt32(Console.ReadLine());
            while (size > incomingList.Count)
            {
                Console.WriteLine("Size too large, enter smaller subset: ");
                size = Convert.ToInt32(Console.ReadLine());
            }

            while (returnList.Count < size)
            {
                randomInt = r.Next(incomingList.Count);
                if (!returnList.Contains(incomingList[randomInt]))
                {
                    returnList.Add(incomingList[randomInt]);
                }
            }

            return returnList;
        }

它接受一个通用的对象列表并返回一个较小的子集。该函数有效并且可以采用 Pet 或 Book 对象。我想在包含 Pet 和 Book 类型的列表上使用相同的函数。我知道如何做到这一点的最好方法是使用接口(继承在这里没有意义)。

interface ISubset<T>
    {

        IEnumerable<T> GetSubset();
    }

当我在我的 Pet 类上实现接口时,它看起来像

  class Pet : ISubset<Pet>

在我的主要课程中,我有一个宠物和书籍清单。我想将这两个对象都添加到ISubset对象列表中,这样我就可以在两者上使用 GetSubset 函数。但是,我不能声明像这样的列表

List<ISubset<T>> list = new List<ISubset<T>>();

我收到错误 。当接口接受泛型类型时,如何声明 ISubset 对象列表'the type or namespace T could not be found

好的,我有两个列表,例如

List<Pet> petList = new List<Pet>();
        petList.Add(new Pet() { Name = "Mr.", Species = "Dog" });
        petList.Add(new Pet() { Name = "Mrs.", Species = "Cat" });
        petList.Add(new Pet() { Name = "Mayor", Species = "Sloth" });
        petList.Add(new Pet() { Name = "Junior", Species = "Rabbit" });


List<Book> bookList = new List<Book>();
        bookList.Add(new Book() { Author = "Me", PageCount = 100, Title = "MyBook" });
        bookList.Add(new Book() { Author = "You", PageCount = 200, Title = "YourBook" });
        bookList.Add(new Book() { Author = "Pat", PageCount = 300, Title = "PatsBook" });

如果我想将这些列表一起添加到另一个列表中,类型应该是什么?

4

2 回答 2

4

您只能使用 a List<object>,但如果您想按照您建议的方式进行操作,那么您需要创建一个带有涵盖您的两个类的签名的接口

public interface IGen
{
    int A;
    int Method;
}

然后在你的类中继承/实现这个接口

public class Pet : IGen
{
    public int A { get; set; }
    private int Method(){ ... }
}

public class Book : IGen
{
    public int A { get; set; }
    private int Method(){ ... }
}

然后你就可以传入你的GetSubSetlike

GetSubSet<IGen>(List<IGen> incomingList) { ... }

我希望这有帮助。

于 2013-07-05T15:40:53.567 回答
1

List<object>应该可以正常工作。

以下是在 LinqPad 中测试的

void Main()
{
    List<Pet> petList = new List<Pet>();
            petList.Add(new Pet() { Name = "Mr.", Species = "Dog" });
            petList.Add(new Pet() { Name = "Mrs.", Species = "Cat" });
            petList.Add(new Pet() { Name = "Mayor", Species = "Sloth" });
            petList.Add(new Pet() { Name = "Junior", Species = "Rabbit" });


    List<Book> bookList = new List<Book>();
            bookList.Add(new Book() { Author = "Me", PageCount = 100, Title = "MyBook" });
            bookList.Add(new Book() { Author = "You", PageCount = 200, Title = "YourBook" });
            bookList.Add(new Book() { Author = "Pat", PageCount = 300, Title = "PatsBook" });

    List<object> both = petList.OfType<object>().Union(bookList.OfType<object>()).ToList().Dump();
}

// Define other methods and classes here
public class Pet
{
  public string Name { get; set; }
  public string Species { get; set; }
}

public class Book
{
  public string Author { get; set; }
  public int PageCount { get; set; }
  public string Title { get; set; }
}
于 2013-07-05T15:46:40.423 回答