Ejemplo n.º 1
0
    private static object DeserializeArray(Type type, List <object> list)
    {
        if (list == null || list.Count == 0)
        {
            return(null);
        }
        if (type.IsArray)
        {
            Type  elementType = type.GetElementType();
            Array v           = Array.CreateInstance(elementType, list.Count);
            for (int i = 0; i < list.Count; i++)
            {
                object child = list[i];
                object item;
                if (child is IDictionary)
                {
                    item = DeserializeObject(elementType, child as Dictionary <string, object>);
                }
                else if (child is IList)
                {
                    item = DeserializeArray(elementType, child as List <object>);
                }
                else
                {
                    item = DeserializeValue(elementType, child);
                }
                v.SetValue(item, i);
            }

            return(v);
        }
        if (OnlineMapsReflectionHelper.IsGenericType(type))
        {
            Type   listType = OnlineMapsReflectionHelper.GetGenericArguments(type)[0];
            object v        = Activator.CreateInstance(type);

            for (int i = 0; i < list.Count; i++)
            {
                object child = list[i];
                object item;
                if (child is IDictionary)
                {
                    item = DeserializeObject(listType, child as Dictionary <string, object>);
                }
                else if (child is IList)
                {
                    item = DeserializeArray(listType, child as List <object>);
                }
                else
                {
                    item = DeserializeValue(listType, child);
                }
                try
                {
                    MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add");
                    if (methodInfo != null)
                    {
                        methodInfo.Invoke(v, new[] { item });
                    }
                }
                catch
                {
                }
            }

            return(v);
        }


        return(null);
    }
Ejemplo n.º 2
0
    public override object Deserialize(Type type)
    {
        if (_count == 0)
        {
            return(null);
        }

        if (type.IsArray)
        {
            Type  elementType = type.GetElementType();
            Array v           = Array.CreateInstance(elementType, _count);
            if (array[0] is OnlineMapsJSONObject)
            {
                IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(elementType, BindingFlags.Instance | BindingFlags.Public);
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem child = array[i];
                    object             item  = (child as OnlineMapsJSONObject).Deserialize(elementType, members);
                    v.SetValue(item, i);
                }
            }
            else
            {
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem child = array[i];
                    object             item  = child.Deserialize(elementType);
                    v.SetValue(item, i);
                }
            }

            return(v);
        }
        if (OnlineMapsReflectionHelper.IsGenericType(type))
        {
            Type   listType = OnlineMapsReflectionHelper.GetGenericArguments(type)[0];
            object v        = Activator.CreateInstance(type);

            if (array[0] is OnlineMapsJSONObject)
            {
                IEnumerable <MemberInfo> members = OnlineMapsReflectionHelper.GetMembers(listType, BindingFlags.Instance | BindingFlags.Public);
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem child = array[i];
                    object             item  = (child as OnlineMapsJSONObject).Deserialize(listType, members);
                    try
                    {
                        MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add");
                        if (methodInfo != null)
                        {
                            methodInfo.Invoke(v, new[] { item });
                        }
                    }
                    catch
                    {
                    }
                }
            }
            else
            {
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem child = array[i];
                    object             item  = child.Deserialize(listType);
                    try
                    {
                        MethodInfo methodInfo = OnlineMapsReflectionHelper.GetMethod(type, "Add");
                        if (methodInfo != null)
                        {
                            methodInfo.Invoke(v, new[] { item });
                        }
                    }
                    catch
                    {
                    }
                }
            }

            return(v);
        }


        return(null);
    }