tags:

views:

2461

answers:

5

Hello to all,

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.

A: 

How about

List<object> collection = new List<object>((IEnumerable)myObject);
Cameron MacFarland
+2  A: 

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();
Andrew Robinson
This one also works. Thank you.
Sergio Romero
This is also creating a new list, rather than casting the original one.
Cameron MacFarland
+1  A: 

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

IList collection = (IList)myObject;
erikkallen
Thank you. This did it.
Sergio Romero
@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).
LukeH
@Luke: If it is a List<T> (which is implied by the question), it will implement IList.
erikkallen
@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.
LukeH
+1  A: 

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.

Jon Skeet
A: 

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);
   }
}
Chris Holmes