Casting object array to MyObject array

Let’s say that I have an ArrayList of MyObject instances.

ArrayList.ToArray() returns an array of object ( object[] ). Now I want to cast the object[] to MyObject[] without creating a new array.

This throws a InvalidCastException:


(MyObject[])arrayList.ToArray()

And this always returns null:


arrayList.ToArray() as MyObject[]

Any other ideas?

Thanks.

Oops, I didn’t see that the ToArray method has an overload that takes a Type. :-[

This seems to do what I want:


(MyObject[])arrayList.ToArray(typeof(MyObject))

I can’t wait for Generics to come :slight_smile: