简体   繁体   English

使用通用Collection类获取其他类的Collection

[英]Get Collection of Other Class using generic Collection class

I have a collection class, let's say UserCollection , which is derived from Collection (base class for a generic collection). 我有一个收集类,比方说UserCollection ,它是从Collection派生的(泛型收集的基类)。

Now I want a UserCollection and here is my code, but it is saying it can not convert from generic List to my collection class: 现在,我需要一个UserCollection ,这是我的代码,但是它不能将其从通用List转换为我的收藏类:

public class User
{
  // All user Fields
}

public class UserCollection : Collection<User>
{
   public static UserCollection GetUserCollection()
   {
       DataTable table = ...//all users data from DB
       List<User> userList = table.ToCollection<User>(); // Call an extension method to convert to          generic List of user
       return userList; // Cannot implicitly convert type 'System.Collections.Generic.List<User>' to 'UserCollection'
   }
}

// ToCollection function is here // ToCollection函数在这里

http://www.codeproject.com/Tips/195889/Convert-Datatable-to-Collection-using-Generic http://www.codeproject.com/Tips/195889/Convert-Datatable-to-Collection-using-Generic

What do I need to make it work? 我需要什么使它起作用?

You could provide an appropriate constructor that calls the base constructor: 您可以提供一个适当的构造函数来调用基本构造函数:

public class UserCollection : Collection<User>
{
    public UserCollection() { }
    public UserCollection(IList<User> users) : base(users) { }

    public static UserCollection GetUserCollection()
    {
        // ...
        List<User> userList = ... 
        return new UserCollection(userList);
    }
}

or you have to return ICollection<User> instead because List<User> is not a child class of Collection<User> or UserCollection but it implements ICollection<User> . 否则您必须返回ICollection<User>因为List<User>不是Collection<User>UserCollection的子类,但是它实现了ICollection<User>

Here is a related question: Cannot implicitly convert List<T> to Collection<T> 这是一个相关的问题: 无法将List <T>隐式转换为Collection <T>

There are two reasons this will not work. 有两个原因导致此方法不起作用。 Primarily, you cannot cast a class to an inherited class. 首先,您不能将类强制转换为继承的类。 UserCollection is a Collection, but Collection is not a UserCollection. UserCollection是一个Collection,但Collection不是UserCollection。 You could assign UserCollection to a Collection variable but no the other way around. 您可以将UserCollection分配给Collection变量,但不能相反。 Additionally, you are receiving a List from the ToCollection method. 此外,您将从ToCollection方法接收一个列表。 You run into the same scenario: Collection<> is not a List<>. 您遇到相同的情况:Collection <>不是List <>。 See http://msdn.microsoft.com/en-us/library/ms173105.aspx for more information about casting. 有关铸造的更多信息,请参见http://msdn.microsoft.com/zh-cn/library/ms173105.aspx

One solution is to implement a constructor that calls the base constructor that takes an IList<> like so: 一种解决方案是实现一个构造函数,该构造函数调用采用IList <>的基本构造函数,如下所示:

public UserCollection(IList<User> users)
    : base(users)
{
}

Then you could modify your GetUserCollection method to call it: 然后,您可以修改GetUserCollection方法以调用它:

public static UserCollection GetUserCollection()
{
   DataTable table = ...//all users data from DB
   List<User> userList = table.ToCollection<User>(); 
   return new UserCollection(userList);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM