Xamarin形成重复对象(深层复制)

时间:2017-01-21 11:00:51

标签: c# web-services xamarin

我尝试使用Xamarin Forms(使用PCL项目)。我的范围是使应用程序使用公共服务(通过Web服务公开)。 我尝试使用Xamarin网站的TODOASMX Example。问题是代码:

static TodoItem FromASMXServiceTodoItem (ASMXService.TodoItem item)
    {
        return new TodoItem {
            ID = item.ID,
            Name = item.Name,
            Notes = item.Notes,
            Done = item.Done
        };
    }

此代码的范围是将数据从ASMX Web服务(ASMXService.TodoItem)复制到私有domanin(TodoItem)。类型是相同的,但在名称空间不同而且类型不同。

在我的情况下,TodoItem类型越来越复杂,我需要使用深层复制。 现在我尝试使用此代码进行深层复制:

public static object CloneObject(object objSource)

    {

        //step : 1 Get the type of source object and create a new instance of that type

        Type typeSource = objSource.GetType();

        object objTarget = Activator.CreateInstance(typeSource);



        //Step2 : Get all the properties of source object type

        PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);



        //Step : 3 Assign all source property to taget object 's properties

        foreach (PropertyInfo property in propertyInfo)

        {

            //Check whether property can be written to 

            if (property.CanWrite)

            {

                //Step : 4 check whether property type is value type, enum or string type

                if (property.PropertyType.IsValueType || property.PropertyType.IsEnum || property.PropertyType.Equals(typeof(System.String)))

                {

                    property.SetValue(objTarget, property.GetValue(objSource, null), null);

                }

                //else property type is object/complex types, so need to recursively call this method until the end of the tree is reached

                else

                {

                    object objPropertyValue = property.GetValue(objSource, null);

                    if (objPropertyValue == null)

                    {

                        property.SetValue(objTarget, null, null);

                    }

                    else

                    {

                        property.SetValue(objTarget, CloneObject(objPropertyValue), null);

                    }

                }

            }

        }

        return objTarget;

    }

但是在运行代码时错误是:

  

System.MissingMethodException:找不到类型TodoASMX.Droid.MeginetOTA.excInfoByLang []

的默认构造函数

现在类型TodoASMX.Droid.MeginetOTA.excInfoByLang []对我来说是不可修改的,我无法将默认构造函数添加到此类型。通过导入公共WebService返回此类型。

感谢任何解决方法(或解决方案)。

非常感谢提前。

MP

1 个答案:

答案 0 :(得分:0)

主要问题是TodoASMX.Droid.MeginetOTA.excInfoByLang[]是一个数组。数组没有无参数构造函数,因为你需要传递它的长度。

您必须单独处理数组:

if (typeSource.IsArray)
{
    var sourceArray = (Array) objSource;
    var length = sourceArray.Length;
    var copyArray = (Array)Activator.CreateInstance(typeSource, length);

    for (var i = 0; i < length; i++)
    {
        var value = sourceArray.GetValue(i);
        copyArray.SetValue(value, i);
    }
}

你的方法可能有点复杂。查看https://stackoverflow.com/a/78612/1489968或搜索已实现的通用克隆库。