Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cast object of type 'System.Object[]' to 'MyObject[]', what gives?

Scenario:

I'm currently writing a layer to abstract 3 similar webservices into one useable class. Each webservice exposes a set of objects that share commonality. I have created a set of intermediary objects which exploit the commonality. However in my layer I need to convert between the web service objects and my objects.

I've used reflection to create the appropriate type at run time before I make the call to the web service like so:

    public static object[] CreateProperties(Type type, IProperty[] properties)
    {
        //Empty so return null
        if (properties==null || properties.Length == 0)
            return null;

        //Check the type is allowed
        CheckPropertyTypes("CreateProperties(Type,IProperty[])",type);

        //Convert the array of intermediary IProperty objects into
        // the passed service type e.g. Service1.Property
        object[] result = new object[properties.Length];
        for (int i = 0; i < properties.Length; i++)
        {
            IProperty fromProp = properties[i];
            object toProp = ReflectionUtility.CreateInstance(type, null);
            ServiceUtils.CopyProperties(fromProp, toProp);
            result[i] = toProp;
        }
        return result;
    }

Here's my calling code, from one of my service implementations:

Property[] props = (Property[])ObjectFactory.CreateProperties(typeof(Property), properties);
_service.SetProperties(folderItem.Path, props);

So each service exposes a different "Property" object which I hide behind my own implementation of my IProperty interface.

The reflection code works in unit tests producing an array of objects whose elements are of the appropriate type. But the calling code fails:

System.InvalidCastException: Unable to cast object of type 'System.Object[]' to type 'MyProject.Property[]

Any ideas?

I was under the impression that any cast from Object will work as long as the contained object is convertable?

like image 564
Rob Stevenson-Leggett Avatar asked Oct 17 '08 14:10

Rob Stevenson-Leggett


2 Answers

Alternative answer: generics.

public static T[] CreateProperties<T>(IProperty[] properties)
    where T : class, new()
{
    //Empty so return null
    if (properties==null || properties.Length == 0)
        return null;

    //Check the type is allowed
    CheckPropertyTypes("CreateProperties(Type,IProperty[])",typeof(T));

    //Convert the array of intermediary IProperty objects into
    // the passed service type e.g. Service1.Property
    T[] result = new T[properties.Length];
    for (int i = 0; i < properties.Length; i++)
    {
        T[i] = new T();
        ServiceUtils.CopyProperties(properties[i], t[i]);
    }
    return result;
}

Then your calling code becomes:

Property[] props = ObjectFactory.CreateProperties<Property>(properties);
_service.SetProperties(folderItem.Path, props);

Much cleaner :)

like image 176
Jon Skeet Avatar answered Nov 13 '22 09:11

Jon Skeet


Basically, no. There are a few, limited, uses of array covariance, but it is better to simply know which type of array you want. There is a generic Array.ConvertAll that is easy enough (at least, it is easier with C# 3.0):

Property[] props = Array.ConvertAll(source, prop => (Property)prop);

The C# 2.0 version (identical in meaning) is much less eyeball-friendly:

 Property[] props = Array.ConvertAll<object,Property>(
     source, delegate(object prop) { return (Property)prop; });

Or just create a new Property[] of the right size and copy manually (or via Array.Copy).

As an example of the things you can do with array covariance:

Property[] props = new Property[2];
props[0] = new Property();
props[1] = new Property();

object[] asObj = (object[])props;

Here, "asObj" is still a Property[] - it it simply accessible as object[]. In C# 2.0 and above, generics usually make a better option than array covariance.

like image 9
Marc Gravell Avatar answered Nov 13 '22 10:11

Marc Gravell