无法转换类型的对象System.Object的[]'到'为MyObject []',怎么办?对象、类型、System、MyObject

2023-09-03 06:31:02 作者:小倔強〆

方案:

我目前正在写一个层来抽象3相似的web服务为一体可用类。每个Web服务公开一组共享通用对象。我创建了一组利用通用的中介物。然而,在我的层我需要到web服务对象和我的对象之间进行转换。

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;
    }

下面是我的呼唤code,从我的服务实现之一:

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

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

因此​​,每个服务公开,我躲在我自己的实现我的iProperty界面的不同的属性对象。

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

反射code工作在生产的对象,其元素为相应类型的数组单元测试。但是,调用code失败:

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:无法   投类型的对象System.Object的[]'   输入   MyProject.Property []

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

任何想法?

我下的是IM pression,从对象的任何施法只要会正常工作所包含的对象是可转换?

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

推荐答案

备选答案:泛型

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;
}

然后您的电话code就变成了:

Then your calling code becomes:

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

干净多了:)

Much cleaner :)