简体   繁体   English

演员表 <object> 到IEnumerable <T> 一般任务中

[英]Cast List<object> to IEnumerable<T> within Generic Task

I have a generic task where I am using a List<object> to add objects of type specified in the task. 我有一个通用任务,其中我使用List<object>添加任务中指定类型的对象。

I would like to return this list by casting it from list<object> to IEnumerable<T> within the task. 我想通过将该列表从list<object>强制转换为IEnumerable<T>来返回此列表。 Is this possible. 这可能吗。

Appreciate any help! 感谢任何帮助!

public List<Object> Contactcollection { get; set; }

Task<IEnumerable<T>> GetAllEntityRecords<T>(string URI, string authorization) where T : new()
{

    JEnumerable<JToken> children = Raw.Children();
    Contactcollection = new List<object>();
    foreach (JToken child in children)
    {
        T _contact = new T();
        _contact = JsonConvert.DeserializeObject<T>(child.ToString());

        Contactcollection.Add(_contact);
    }

    return (IEnumerable<T>)Contactcollection;
}

You can use OfType<T> LINQ extension to filter the collection for only the instances of T . 您可以使用OfType<T> LINQ扩展仅对T的实例进行过滤。

Contactcollection.OfType<T>();

However, instead of List<object> you can just use List<T> in this case and avoid all the casting :-) . 但是,在这种情况下,您可以只使用List<T>来代替List<object> ,并避免所有强制转换:-)。

Task<IEnumerable<T>> GetAllEntityRecords<T>(string URI, string authorization) where T : new()
{

    JEnumerable<JToken> children = Raw.Children();
    var results = new List<T>();
    foreach (JToken child in children)
    {            
        var result = JsonConvert.DeserializeObject<T>(child.ToString());

        results.Add(result);
    }
    Contactcollection = new List<object>(results.OfType<object>());
    return results;
}

The question is why does your Contactcollection property have type of List<object> however. 问题是,为什么您的Contactcollection属性具有List<object>类型。 If you know it is going to contain contacts, why not use explicit typing instead of generics? 如果您知道它将包含联系人,为什么不使用显式键入而不是泛型? And if it is not, it would be better to name it differently and then just assign it at the end of the method if you need to store it, as I demonstrated at the end of my sample. 如果不是,最好使用不同的名称,然后在需要存储时在方法末尾进行分配,正如我在样本末尾所演示的那样。

Finally note that you don't have to create a new instance of T before deserializing. 最后请注意,在反序列化之前不必创建Tnew实例。 JsonConvert will instantiate the type automatically. JsonConvert将自动实例化类型。

You can do this quite simply with LINQ. 您可以使用LINQ非常简单地完成此操作。 There's no need to store the results in a list: 无需将结果存储在列表中:

IEnumerable<T> GetAllEntityRecords<T>(string URI, string authorization)
{
  return Raw.Children().Select(child => JsonConvert.DeserializeObject<T>(child.ToString());
}

But if you prefer to store them in a list for some reason, you can add a .ToList() to the end of that expression. 但是,如果由于某种原因您希望将它们存储在列表中,则可以在该表达式的末尾添加.ToList()

演员收到 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>

暂无
暂无

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

相关问题 通用列表 <T> 作为IEnumerable <object> - Generic List<T> as IEnumerable<object> 将通用列表\\转换为通用IEnumerable - Convert\Cast a generic list to a generic IEnumerable 投IEnumerable <object> 列表 <object> - Cast IEnumerable<object> to List<object> 如何从任务转换<generic.list<t> &gt; 到一个 Generic.IEnumerable<t> ? </t></generic.list<t> - How can I convert from a Task<Generic.List<T>> to a Generic.IEnumerable<T>? 将对象转换为通用列表 - Cast an object into generic list 将 Object 转换为通用列表 - Cast Object to Generic List 演员收到 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> Cast List之间的区别是什么? <object> 和Cast IEnumerable <object> - What is the difference between Cast List<object> and Cast IEnumerable<object> 如果T是IEnumerable而无需强制转换,则遍历泛型T - iterating through generic type T if T is IEnumerable without cast 用于将对象列表转换为 IEnumerable 的通用类<SqlDataRecord> - Generic class for convert list of object to IEnumerable<SqlDataRecord>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM