Cast received object to a List<object> or IEnumerable<object>

后端 未结 8 1571
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 23:52

I\'m trying to perform the following cast

private void MyMethod(object myObject)  
{  
    if(myObject is IEnumerable)  
    {
        List c         


        
      
      
      
8条回答
  •  独厮守ぢ
    2020-12-08 00:23

    You can't cast an IEnumerable to a List.

    But you can accomplish this using LINQ:

    var result = ((IEnumerable)myObject).Cast().ToList();
    
        

    提交回复
    热议问题