Beispiel #1
0
        public static T ToObjectArray <T>(object input)
        {
            Type          typeFromHandle = typeof(T);
            List <object> list           = input as List <object>;
            object        obj            = null;

            if (list == null || (list.Count == 1 && list[0] == null))
            {
                return((T)((object)obj));
            }
            obj = Activator.CreateInstance(typeFromHandle, new object[]
            {
                list.Count
            });
            Type elementType = obj.GetType().GetElementType();

            for (int i = 0; i < list.Count; i++)
            {
                object obj2;
                if (elementType == typeof(string))
                {
                    obj2 = new string[1];
                }
                else
                {
                    obj2 = Activator.CreateInstance(elementType);
                }
                try
                {
                    Json.DataType dataType = Json.checkObjectType(list[i]);
                    if (dataType == Json.DataType.String)
                    {
                        obj2 = (list[i] as string);
                    }
                    else
                    {
                        if (dataType == Json.DataType.Object)
                        {
                            Json.ToObject(obj2, list[i]);
                        }
                        else
                        {
                            if (dataType == Json.DataType.Int || dataType == Json.DataType.Float)
                            {
                                if (elementType == typeof(float))
                                {
                                    obj2 = (float)list[i];
                                }
                                else
                                {
                                    obj2 = (int)list[i];
                                }
                            }
                            else
                            {
                                obj2 = list[i];
                            }
                        }
                    }
                    ((Array)obj).SetValue(obj2, i);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Concat(new object[]
                    {
                        ex.Message,
                        "\nArray Member mismatch: [",
                        typeFromHandle.Name,
                        "] ReqType is ",
                        elementType,
                        " But ",
                        Json.checkObjectType(list[i]).ToString().ToString(),
                        " data Comes"
                    }));
                }
            }
            return((T)((object)obj));
        }
Beispiel #2
0
        public static object ToObject(object output, object input)
        {
            Thread.Sleep(0);
            if (input == null)
            {
                output = null;
                return(output);
            }
            string text = "DestType:" + output.GetType().ToString() + " >> ";

            FieldInfo[] fields = output.GetType().GetFields();
            Dictionary <string, object> dictionary = input as Dictionary <string, object>;

            FieldInfo[] array = fields;
            for (int i = 0; i < array.Length; i++)
            {
                FieldInfo fieldInfo = array[i];
                if (dictionary.ContainsKey(fieldInfo.Name))
                {
                    if (dictionary[fieldInfo.Name] != null)
                    {
                        Json.DataType dataType = Json.checkObjectType(dictionary[fieldInfo.Name]);
                        if (dataType != Json.DataType.Unknown)
                        {
                            try
                            {
                                Type   fieldType = fieldInfo.FieldType;
                                object obj       = null;
                                bool   flag      = false;
                                switch (dataType)
                                {
                                case Json.DataType.Int:
                                {
                                    object obj2 = (int)dictionary[fieldInfo.Name];
                                    if (fieldType == typeof(string))
                                    {
                                        obj = obj2.ToString();
                                    }
                                    else
                                    {
                                        if (fieldType == typeof(bool))
                                        {
                                            bool flag2 = (int)obj2 != 0;
                                            obj = flag2;
                                        }
                                        else
                                        {
                                            if (fieldType.IsArray)
                                            {
                                                obj = Activator.CreateInstance(fieldType, new object[]
                                                    {
                                                        1
                                                    });
                                                Type   elementType = obj.GetType().GetElementType();
                                                object value       = Activator.CreateInstance(elementType);
                                                int    num         = (int)obj2;
                                                value = num;
                                                ((Array)obj).SetValue(value, 0);
                                            }
                                            else
                                            {
                                                obj = obj2;
                                            }
                                        }
                                    }
                                    flag = true;
                                    break;
                                }

                                case Json.DataType.Float:
                                {
                                    object obj3 = (float)dictionary[fieldInfo.Name];
                                    if (fieldType == typeof(string))
                                    {
                                        obj = (obj3 as string);
                                    }
                                    else
                                    {
                                        if (fieldType == typeof(int))
                                        {
                                            float num2 = (float)obj3;
                                            obj = (int)num2;
                                        }
                                        else
                                        {
                                            if (fieldType.IsArray)
                                            {
                                                obj = Activator.CreateInstance(fieldType, new object[]
                                                    {
                                                        1
                                                    });
                                                Type   elementType2 = obj.GetType().GetElementType();
                                                object value2       = Activator.CreateInstance(elementType2);
                                                float  num3         = (float)obj3;
                                                value2 = num3;
                                                ((Array)obj).SetValue(value2, 0);
                                            }
                                            else
                                            {
                                                obj = obj3;
                                            }
                                        }
                                    }
                                    flag = true;
                                    break;
                                }

                                case Json.DataType.Boolean:
                                    if (fieldType == typeof(bool))
                                    {
                                        obj  = (bool)dictionary[fieldInfo.Name];
                                        flag = true;
                                    }
                                    break;

                                case Json.DataType.String:
                                {
                                    string text2 = dictionary[fieldInfo.Name] as string;
                                    if (fieldType == typeof(string))
                                    {
                                        obj = text2;
                                    }
                                    else
                                    {
                                        if (fieldType == typeof(int))
                                        {
                                            int num4 = 0;
                                            if (int.TryParse(text2, out num4))
                                            {
                                                obj = num4;
                                            }
                                            else
                                            {
                                                obj = 0;
                                            }
                                        }
                                        else
                                        {
                                            if (fieldType == typeof(float))
                                            {
                                                float num5 = 0f;
                                                if (float.TryParse(text2, out num5))
                                                {
                                                    obj = float.Parse(text2);
                                                }
                                                else
                                                {
                                                    obj = 0f;
                                                }
                                            }
                                            else
                                            {
                                                if (fieldType.IsEnum)
                                                {
                                                    if (Enum.IsDefined(fieldType, text2.ToString()))
                                                    {
                                                        obj = Enum.Parse(fieldType, text2.ToString());
                                                    }
                                                }
                                                else
                                                {
                                                    if (fieldType.IsArray)
                                                    {
                                                        obj = Activator.CreateInstance(fieldType, new object[]
                                                            {
                                                                1
                                                            });
                                                        ((Array)obj).SetValue(text2, 0);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    flag = true;
                                    break;
                                }

                                case Json.DataType.Array:
                                    if (fieldType.IsArray)
                                    {
                                        List <object> list = dictionary[fieldInfo.Name] as List <object>;
                                        if (list != null && (list.Count != 1 || list[0] != null))
                                        {
                                            obj = Activator.CreateInstance(fieldType, new object[]
                                            {
                                                list.Count
                                            });
                                            Type elementType3 = obj.GetType().GetElementType();
                                            for (int j = 0; j < list.Count; j++)
                                            {
                                                Thread.Sleep(0);
                                                object obj4;
                                                if (elementType3 == typeof(string))
                                                {
                                                    obj4 = new string[1];
                                                }
                                                else
                                                {
                                                    obj4 = Activator.CreateInstance(elementType3);
                                                }
                                                try
                                                {
                                                    Json.DataType dataType2 = Json.checkObjectType(list[j]);
                                                    if (dataType2 == Json.DataType.String)
                                                    {
                                                        obj4 = (list[j] as string);
                                                    }
                                                    else
                                                    {
                                                        if (dataType2 == Json.DataType.Object)
                                                        {
                                                            Json.ToObject(obj4, list[j]);
                                                        }
                                                        else
                                                        {
                                                            if (dataType2 == Json.DataType.Int || dataType2 == Json.DataType.Float)
                                                            {
                                                                if (elementType3 == typeof(float))
                                                                {
                                                                    obj4 = (float)list[j];
                                                                }
                                                                else
                                                                {
                                                                    obj4 = (int)list[j];
                                                                }
                                                            }
                                                            else
                                                            {
                                                                obj4 = list[j];
                                                            }
                                                        }
                                                    }
                                                    ((Array)obj).SetValue(obj4, j);
                                                }
                                                catch (Exception var_29_560)
                                                {
                                                    Debug.LogException(var_29_560);
                                                }
                                            }
                                            flag = true;
                                        }
                                    }
                                    break;

                                case DataType.Object:
                                {
                                    object output2 = Activator.CreateInstance(fieldType);
                                    obj  = ToObject(output2, dictionary[fieldInfo.Name]);
                                    flag = true;
                                    break;
                                }

                                default:
                                    obj = Activator.CreateInstance(fieldType);
                                    break;
                                }
                                if (flag)
                                {
                                    fieldInfo.SetValue(output, obj);
                                }
                            }
                            catch (KeyNotFoundException)
                            {
                            }
                        }
                    }
                }
            }
            return(output);
        }