繁体   English   中英

Cast List之间的区别是什么? <object> 和Cast IEnumerable <object>

[英]What is the difference between Cast List<object> and Cast IEnumerable<object>

我试图从对象(该对象包含List<Apple> )转换为List<object> ,但它失败并出现异常:

无法将类型为“System.Collections.Generic.List [list_cast_object_test.Apple]”的对象强制转换为“System.Collections.Generic.List [System.Object]”。

当我用IEnumerableIList (一个接口)替换List ,它运行良好,我不明白它的区别....

代码如下:

    private void Form1_Load(object sender, EventArgs e) {
        object resultObject = GetListAsObject();

        // this cast works fine, it's normal...
        var resAsIt = (List<Apple>)resultObject;

        // also when I use a generic interface of 'object' cast works fine
        var resAsIEnumerable = (IEnumerable<object>)resultObject;

        // but when I use a generic class of 'object' it throws me error: InvalidCastException
        var resAsList = (List<object>)resultObject;
    }

    private object GetListAsObject() {
        List<Apple> mere = new List<Apple>();
        mere.Add(new Apple { Denumire = "ionatan", Culoare = "rosu" });
        mere.Add(new Apple { Denumire = "idared", Culoare = "verde" });
        return (object)mere;
    }
}
public class Apple {
    public string Denumire { get; set; }
    public string Culoare { get; set; }
}

有人可以解释一下它发生了什么? 强制转换为通用接口和强制转换为泛型类有什么区别?

这是因为在IEnumerable<T> T是协变的,而在List<T> T中不支持协方差。

在c#中查看有关Covariance和Contraviance的更多信息

如果你想做那样的事情,你必须使用ToList<T>()重载,它将它包装到对象类型,如:

 List<object> result = resultObject.ToList<object>();

由于新的List<T>创建,我们能够将List<Apple>转换为List<Object> ,其中TObject类型但我们不能将原始的List<Apple>引用转换为List<object>

在这种情况下,它会创建的实例Object类型以及将存储参考你的类型为类型的参考object ,但值类型或strcuts的情况下,将有拳击成本。

IEnumerable<T>是一个接口,由于类型T被定义为协变(它实际上是IEnumerable<out T> ,请注意out ),你可以将T转换为基本实现,因此IEnumerable<object是一个有效的强制转换您的List<Apple>类型List<Apple>

但是,类型List<Apple>不等于List<object>类型,列表List<Apple>也不是从List<object>派生的。 List<T>上的类型参数不是协变的,因此强制转换是无效的。

演员收到 object 到列表<object>或 IEnumerable<object><div id="text_translate"><p> 我正在尝试执行以下演员</p><pre>private void MyMethod(object myObject) { if(myObject is IEnumerable) { List&lt;object&gt; collection = (List&lt;object&gt;)myObject; ... do something } else {... do something } }</pre><p> 但我总是以以下异常结束:</p><p> 无法将“System.Collections.Generic.List 1[MySpecificType]' to type 'System.Collections.Generic.List</p><p> 我真的需要这个工作,因为这个方法需要非常通用才能接收单个对象和 collections 两种未指定的类型。</p><p> 这是可能的,还是有另一种方法可以做到这一点。</p><p> 谢谢你。</p></div></object></object>

[英]Cast received object to a List<object> or IEnumerable<object>

将 object 投到列表中<object><div id="text_translate"><p>由于某些我无法控制的原因,需要定义我的一种方法来接受 object 类型的一个参数。 但我知道这个参数的值实际上是 List 类型,带有一个我不知道的通用参数。</p><p> 我正在尝试将此 object 转换为 List&lt;object&gt;,但不知道如何在不知道确切的通用参数是什么的情况下执行此操作。</p><p> 我想做这样的事情:</p><pre> public static List&lt;object&gt; ConvertToList(object input) { if (input.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List&lt;&gt;))) { //Of course this does not work, because input might for example be of type List&lt;string&gt; return (List&lt;object&gt;) input; } else { return null; } }</pre><p> 我知道,如果我有List&lt;T&gt;而不是object input ,使用以下方法会很容易:</p><pre> return input.Cast&lt;object&gt;()</pre><p> 但不幸的是,我坚持使用 object 参数。 知道如何解决这个问题吗?</p></div></object>

[英]Cast object to List<object>

暂无
暂无

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

相关问题 投IEnumerable <object> 列表 <object> 演员收到 object 到列表<object>或 IEnumerable<object><div id="text_translate"><p> 我正在尝试执行以下演员</p><pre>private void MyMethod(object myObject) { if(myObject is IEnumerable) { List&lt;object&gt; collection = (List&lt;object&gt;)myObject; ... do something } else {... do something } }</pre><p> 但我总是以以下异常结束:</p><p> 无法将“System.Collections.Generic.List 1[MySpecificType]' to type 'System.Collections.Generic.List</p><p> 我真的需要这个工作,因为这个方法需要非常通用才能接收单个对象和 collections 两种未指定的类型。</p><p> 这是可能的,还是有另一种方法可以做到这一点。</p><p> 谢谢你。</p></div></object></object> null转换和默认对象之间有什么区别 将对象转换为IEnumerable <object> ? 从IEnumerable转换为IEnumerable <object> 将对象转换为IEnumerable 如何投射IEnumerable <object> 到一个IEnumerable <runtime type> 为什么IEnumerable <struct> 转换为IEnumerable <object> ? 将 object 投到列表中<object><div id="text_translate"><p>由于某些我无法控制的原因,需要定义我的一种方法来接受 object 类型的一个参数。 但我知道这个参数的值实际上是 List 类型,带有一个我不知道的通用参数。</p><p> 我正在尝试将此 object 转换为 List&lt;object&gt;,但不知道如何在不知道确切的通用参数是什么的情况下执行此操作。</p><p> 我想做这样的事情:</p><pre> public static List&lt;object&gt; ConvertToList(object input) { if (input.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List&lt;&gt;))) { //Of course this does not work, because input might for example be of type List&lt;string&gt; return (List&lt;object&gt;) input; } else { return null; } }</pre><p> 我知道,如果我有List&lt;T&gt;而不是object input ,使用以下方法会很容易:</p><pre> return input.Cast&lt;object&gt;()</pre><p> 但不幸的是,我坚持使用 object 参数。 知道如何解决这个问题吗?</p></div></object> 将对象强制转换为IEnumerable <T> 我知道T是什么
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM