如何投放名单,LT; ClassB的>列出<&的ClassA GT;当从ClassB的继承的ClassA? [英] How to cast List<ClassB> to List<ClassA> when ClassB inherits from ClassA?

查看:112
本文介绍了如何投放名单,LT; ClassB的>列出<&的ClassA GT;当从ClassB的继承的ClassA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我反序列化JSON字符串列表< ClassB的> ,现在我想将它转换为列表< ClassA的> 之前,我从
返回其 BindModel 方法。我需要铸造,因为这些方法期望得到列表< ClassA的>



为什么我得到错误,而铸造?毕竟, ClassB的 ClassA的继承。我应该怎么办?



P.S。这个问题是从的这个帖子。在行新DataContractJsonSerializer(typeof运算(列表< ClassB的>)); 而不是列表与LT; ClassB的> 的类型将是在运行时构建的。

 公众覆盖对象BindModel(...)
{
无功序列化=新DataContractJsonSerializer(typeof运算(列表<&ClassB的GT;));
的MemoryStream毫秒​​=新的MemoryStream(Encoding.UTF8.GetBytes([{\id\:\1\,\name\:\name\ }]));
无功名单= serializer.ReadObject(毫秒);

返回(列表<&的ClassA GT;)名单;
}

[KnownType(typeof运算(ClassA的))]
[DataContract]
公共类ClassA的
{
公共ClassA的();
}

[KnownType(typeof运算(ClassB的))]
[DataContract]
公共类ClassB的:ClassA的
{
[数据成员( NAME =ID)]
公众诠释标识{搞定;组; }
[数据成员(名称=名称)]
公共字符串类别名称{搞定;组; }
}


解决方案

您可以使用

 演员LT; T>()

例如:

 列表< A> listOfA =新的List< B>()演员LT; A>(); 

这其实就是逊色的LINQ ,并在<$ C实现$ C>的IEnumerable ,而不是的IEnumerable< T> ,但仍然是非常有用的。它的效率不高,因为正如我所说,它试图投它。



记住列表不允许这是一个麻烦。最好是使用的IEnumerable< T> 的接口,而不是列表



您可以说:



 的IEnumerable< b> listOfB =新的List< B>(); 
IEnumerable的< A> listOfA = listOfB; //不需要铸造


I deserialized json string to List<ClassB> and now I want to cast it to List<ClassA> before I return it from BindModel method. I need casting because the methods expects to get List<ClassA>.

Why I get error while casting? After all, ClassB inherits from ClassA. What should I do?

P.S. this question is extended from this post. In line new DataContractJsonSerializer(typeof(List<ClassB>)); instead of List<ClassB> the type will be constructed at runtime.

    public override object BindModel(...)
    {
          var serializer = new DataContractJsonSerializer(typeof(List<ClassB>));
          MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("[{\"id\":\"1\",\"name\":\"name\"}]"));
          var list = serializer.ReadObject(ms);

          return (List<ClassA>)list;
    }

    [KnownType(typeof(ClassA))]
    [DataContract]
    public class ClassA
    {
        public ClassA();
    }

    [KnownType(typeof(ClassB))]       
    [DataContract]
    public class ClassB : ClassA
    {
        [DataMember(Name = "id")]
        public int Id { get; set; }
        [DataMember(Name = "name")]
        public string CategoryName { get; set; }
    }

解决方案

You can use

 Cast<T>()

For example:

 List<A> listOfA = new List<B>().Cast<A>();

This in fact is inferior to Linq and is implemented on IEnumerable rather than IEnumerable<T> but still is useful. It is not efficient since as I said, it tries to cast it.

Remember List does not allow for covariance which is a nuisance. It is preferable to use IEnumerable<T> as interface rather than List.

You can say:

 IEnumerable<B> listOfB = new List<B>();
 IEnumerable<A> listOfA = listOfB; // no casting required

这篇关于如何投放名单,LT; ClassB的&GT;列出&LT;&的ClassA GT;当从ClassB的继承的ClassA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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