Cast received object to a List<object> or IEnumerable<object>
Asked Answered
L

10

88

I'm trying to perform the following cast

private void MyMethod(object myObject)  
{  
    if(myObject is IEnumerable)  
    {
        List<object> collection = (List<object>)myObject;  
        ... do something   
    }  
    else  
    {  
        ... do something  
    }  
}

But I always end up with the following excepction:

Unable to cast object of type 'System.Collections.Generic.List1[MySpecificType]' to type 'System.Collections.Generic.List1[System.Object]'

I really need this to work because this method needs to be very generic to receive single objects and collections both of unspecified types.

Is this possible, or is there another way of accomplishing this.

Thank you.

Lovering answered 10/3, 2009 at 22:42 Comment(0)
L
101

C# 4 will have covariant and contravariant template parameters, but until then you have to do something nongeneric like

IList collection = (IList)myObject;
Lanai answered 10/3, 2009 at 22:51 Comment(4)
@Sergio, You should check that myObject implements IList rather than IEnumerable before casting if you want to avoid the risk of runtime errors. Many built-in collections implement IEnumerable but not IList (eg, Dictionary<>, HashSet<>, Hashtable, Queue, Stack etc).Diner
@Luke: If it is a List<T> (which is implied by the question), it will implement IList.Lanai
@erikkallen, True, but this is something that the OP should be aware of. Avoiding the problem is simply a case of changing "if (myObject is IEnumerable)" to "if (myObject is IList)" in the example given.Diner
Or even more concisely (if using IEnumerable), Items.As<IEnumerable>()?.Cast<object>() ?? Enumerable.Empty<object>(), which says: Get this list of generic objects as just a list of objects if the list implements IEnumerable and the result is not null; otherwise, just get an empty enumeration of objects.Membrane
G
92

You can't cast an IEnumerable<T> to a List<T>.

But you can accomplish this using LINQ:

var result = ((IEnumerable)myObject).Cast<object>().ToList();
Genethlialogy answered 10/3, 2009 at 22:50 Comment(2)
This is also creating a new list, rather than casting the original one.Calculating
I had to use (IEnumerable<object>) rather than just (IEnumerable) but otherwise worked fine. I used dotnetcore 3.0Minimus
G
15

This Code worked for me

List<Object> collection = new List<Object>((IEnumerable<Object>)myObject);
Gingivitis answered 29/11, 2016 at 13:33 Comment(1)
Noice. Simple, elegant, single line 🙂Stantonstanway
L
12

Problem is, you're trying to upcast to a richer object. You simply need to add the items to a new list:

if (myObject is IEnumerable)
{
   List<object> list = new List<object>();
   var enumerator = ((IEnumerable) myObject).GetEnumerator();
   while (enumerator.MoveNext())
   {
      list.Add(enumerator.Current);
   }
}
Ladykiller answered 10/3, 2009 at 22:57 Comment(0)
C
10

Do you actually need more information than plain IEnumerable gives you? Just cast it to that and use foreach with it. I face exactly the same situation in some bits of Protocol Buffers, and I've found that casting to IEnumerable (or IList to access it like a list) works very well.

Crowfoot answered 10/3, 2009 at 22:54 Comment(0)
C
8

How about

List<object> collection = new List<object>((IEnumerable)myObject);
Calculating answered 10/3, 2009 at 22:48 Comment(0)
H
3

Have to join the fun...

    private void TestBench()
    {
        // An object to test
        string[] stringEnumerable = new string[] { "Easy", "as", "Pi" };

        ObjectListFromUnknown(stringEnumerable);
    }

    private void ObjectListFromUnknown(object o)
    {
        if (typeof(IEnumerable<object>).IsAssignableFrom(o.GetType()))
        {
            List<object> listO = ((IEnumerable<object>)o).ToList();
            // Test it
            foreach (var v in listO)
            {
                Console.WriteLine(v);
            }
        }
    }
Hairline answered 8/3, 2017 at 20:26 Comment(0)
G
1

Nowadays it's like:

var collection = new List<object>(objectVar);
Gerdagerdeen answered 24/12, 2020 at 9:26 Comment(0)
R
0

You need to type cast using as operator like this.

private void MyMethod(object myObject)  
{  
if(myObject is IEnumerable)  
{
    List<object> collection = myObject as(List<object>); 
    ... do something   
}  
else  
{  
    ... do something  
}  
}      
Risner answered 15/5, 2017 at 7:49 Comment(0)
H
0

You could also convert object to a list of your DTO.

First serialize myObject and then deserilize it into your List.

Here is the code:

List<MyDto> myList = JsonConvert.DeserializeObject<List<MyDto>>(JsonConvert.SerializeObject(myObject));

The Newtonsoft.Json is used in the above code.

Hollywood answered 25/5, 2022 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.