繁体   English   中英

为什么不string.Cast <object> 演员名单 <string> 列表 <object> ?

[英]Why doesn't strings.Cast<object> cast List<string> to List<object>?

这个问题中 ,有人建议我可以使用.Cast<object>将一个泛型集合向上转换为一个对象集合。 读了一下.Cast<> ,我仍然无法将它变成一个通用集合来转换成另一个泛型集合。 以下为什么不工作?

using System.Collections.Generic;
using System.Linq;
using System;

namespace TestCast2343
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> strings = new List<string> { "one", "two", "three" };

            //gives error: cannot convert from 'System.Collections.Generic.List<string>'
            //to 'System.Collections.Generic.List<object>'
            //IEnumerable<string> items = strings.Cast<object>();

            //this works
            strings.Cast<object>();

            //but they are still strings:
            foreach (var item in strings)
            {
                System.Console.WriteLine(item.GetType().Name);
            }

            //gives error: cannot convert from 'System.Collections.Generic.List<string>'
            //to 'System.Collections.Generic.List<object>'
            ProcessCollectionDynamicallyWithReflection(strings);

            Console.ReadLine();
        }

        static void ProcessCollectionDynamicallyWithReflection(List<object> items)
        {
            //...
        }
    }

}

回答:

谢谢里德,这是我开始工作的代码:

using System.Collections.Generic;
using System.Linq;
using System;

namespace TestCast2343
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> strings = new List<string> { "one", "two", "three" };
            List<int> ints = new List<int> { 34, 35, 36 };
            List<Customer> customers = Customer.GetCustomers();

            ProcessCollectionDynamicallyWithReflection(strings.Cast<object>().ToList());
            ProcessCollectionDynamicallyWithReflection(ints.Cast<object>().ToList());
            ProcessCollectionDynamicallyWithReflection(customers.Cast<object>().ToList());

            Console.ReadLine();
        }

        static void ProcessCollectionDynamicallyWithReflection(List<object> items)
        {
            foreach (var item in items)
            {
                Console.WriteLine(item.GetType().Name);
            }
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
            customers.Add(new Customer { FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
            customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson", ZipCode = "23111" });
            customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
            customers.Add(new Customer { FirstName = "Jean", LastName = "Anderson", ZipCode = "16623" });
            return customers;
        }
    }
}

你在滥用Cast<T>

首先,这里:

IEnumerable<string> items = strings.Cast<object>();

当你调用strings.Cast<object>() ,这将返回IEnumerable<object> ,而不是IEnumerable<string> 但是,集合中的项仍然是字符串,但保留在对象的引用中。

稍后,当您想将此传递给一个采用List<object> ,您需要将IEnumerable<T>转换为IList<T> 这很容易就像这样:

// Cast to IEnumerabe<object> then convert to List<object>
ProcessCollectionDynamicallyWithReflection(strings.Cast<object>().ToList());

这是因为Cast<>方法不返回List<T>类型,而是返回IEnumerable<T> 将.ToList调用添加到结尾,它将解决问题。

strings.Cast<object>().ToList();

您还可以从另一个角度解决转换问题:修复ProcessCollectionDynamicallyWithReflection因为它具有不必要的限制:

private static void ShowItemTypes(IEnumerable items)
{
    foreach (object item in items)
    {
        string itemTypeName = (item != null) ? item.GetType().Name : "null";
        Console.WriteLine(itemTypeName);
    }
}

这应该工作:

IEnumerable<object> items = strings.Cast<object>();

此外,如果没有强制转换,则无法将IEnumerable作为List传递到ProcessCollectionDynamicallyWithReflection中。 所以,这甚至更好:

List<object> items = strings.Cast<object>().ToList();

在这个问题中,有人建议我可以使用.Cast<object>将一个泛型集合向上转换为一个对象集合

不,这不正确。 Cast方法不会转换集合,它会转换集合中的每个项目,返回包含转换值的新集合。

您正在创建对象集合,但是您只需将其丢弃,因为您没有将其分配给变量。 (好吧,你实际上丢弃了一个能够创建集合的表达式,但这并不是那么相关。)

您需要将集合分配给变量以保留它,并且因为您需要List<object>您需要将演员项放在列表中:

List<object> objects = strings.Cast<object>.ToList();

也许您应该考虑使用泛型而不是转换为对象:

static void ProcessCollectionDynamicallyWithReflection<T>(List<T> items)

这样,您可以在方法中编写强类型代码,并且您不必创建新集合只是为了能够将其发送到方法。

将 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>

为什么我不能从列表中投射<myclass>列出<object> ?<div id="text_translate"><p> 我有一个对象列表,它们属于我的QuoteHeader类型,我想将此列表作为对象列表传递给能够接受List&lt;object&gt;的方法。</p><p> 我的代码行显示...</p><pre> Tools.MyMethod((List&lt;object&gt;)MyListOfQuoteHeaders);</pre><p> 但是我在设计时收到以下错误...</p><pre> Cannot convert type 'System.Collections.Generic.List&lt;MyNameSpace.QuoteHeader&gt;' to 'System.Collections.Generic.List&lt;object&gt;'</pre><p> 我需要对我的 class 做任何事情来允许这样做吗? 我认为所有类都继承自 object 所以我不明白为什么这不起作用?</p></div></object></myclass>

[英]Why can't I cast from a List<MyClass> to List<object>?

暂无
暂无

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

相关问题 将物件投放至清单<T> 演员表 <object> 列出 <string> 如何将此字符串列表转换为对象 将 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> 为什么我不能从列表中投射<myclass>列出<object> ?<div id="text_translate"><p> 我有一个对象列表,它们属于我的QuoteHeader类型,我想将此列表作为对象列表传递给能够接受List&lt;object&gt;的方法。</p><p> 我的代码行显示...</p><pre> Tools.MyMethod((List&lt;object&gt;)MyListOfQuoteHeaders);</pre><p> 但是我在设计时收到以下错误...</p><pre> Cannot convert type 'System.Collections.Generic.List&lt;MyNameSpace.QuoteHeader&gt;' to 'System.Collections.Generic.List&lt;object&gt;'</pre><p> 我需要对我的 class 做任何事情来允许这样做吗? 我认为所有类都继承自 object 所以我不明白为什么这不起作用?</p></div></object></myclass> 投射列表对象 将 Object 转换为通用列表 如何将对象投射到列表 将对象转换为通用列表 无法将物件投放至清单
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM