将泛型列表转换为从列表派生的类 [英] Casting of Generic List to a Class derived from a List

查看:44
本文介绍了将泛型列表转换为从列表派生的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个从List派生的类。但是,当我尝试将类强制转换为另一个List对象时,出现运行时错误。我的代码类似于下面发布的代码:

I created a class derived from List. However when I tried casting the class to another List object I get a runtime error. My code is similar to the one posted below:

public class CategoryObj
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
    public string Description { get; set; }
}

public class CategoryObjCollection:List<CategoryObj>
{

}

public void Test()
{
        CategoryObjCollection cat = new CategoryObjCollection();
        using (NWDataContext db = new NWDataContext())
        {
            var qry = (from c in db.Categories
                       select new CategoryObj
                       {
                           CategoryID = c.CategoryID,
                           CategoryName = c.CategoryName,
                           Description = c.Description
                       }).ToList();
            cat = (CategoryObjCollection)qry; //runtime error Unable to cast object of type 'System.Collections.Generic.List`1[WindowsFormsApplication1.CategoryObj]' to type 'WindowsFormsApplication1.CategoryObjCollection'.
        }
}

我希望有人可以对此提供帮助。谢谢。

I hope someone can help me on this. Thanks.

推荐答案

为什么希望能够投射?对象不是 CategoryObjCollection 的实例。引用类型强制转换(实际上是强制转换而不是调用显式转换)是要告诉编译器,您认为对象的执行时间类型实际上与您指定的类型兼容,这样您就可以使用该特定类型的成员(在执行时间测试之后)。在这种情况下, ToList 扩展方法只是创建一个新的 List< T>

Why would you expect to be able to cast? The object isn't an instance of CategoryObjCollection. The point of reference type casts (when they're actually casting rather than invoking explicit conversions) is to tell the compiler that you believe the execution time type of the object is actually compatible with the type you specify, so that you can use members of that specific type (after the execution time test). In this case, the ToList extension method just creates a new List<T>.

更重要的是,您首先要实现的 CategoryObjCollection 类型是什么?如果它具有除正常列表以外的任何状态,那么您期望该状态来自LINQ查询之后的何处?如果没有其他任何状态,是否真的会带来任何好处?也许您的类型实际上应该包含一个列表,而不是衍生自该列表?然后,您可以使用查询结果创建该类型的新实例。

More to the point, what is your CategoryObjCollection type meant to achieve in the first place? If it has any state other than the normal list, where would you expect that state to come from after your LINQ query? If it doesn't have any other state, is it really adding any benefit? Maybe your type should actually contain a list rather than deriving from it? Then you could create a new instance of the type using the results of the query.

通常,通常从 List<派生出设计气味; T> 。它没有提供许多方法来专门化其提示性(不同于 Collection< T> )。通常这表明您应该在考虑合成而不是继承。

In general, it's usually a design smell to derive from List<T>. It doesn't provide many ways to specialize its "listiness" (unlike, say, Collection<T>). It's usually a sign that you should be thinking of composition instead of inheritance.

这篇关于将泛型列表转换为从列表派生的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆