コード例 #1
0
ファイル: XMLSerializer.cs プロジェクト: imclab/Unity3DTable
    //---------------------------------------------------------------------------------
    //  DeserializeObject
    //---------------------------------------------------------------------------------
    private static object DeserializeObject(XMLInStream stream, Type type)
    {
        object data = Activator.CreateInstance(type);

        System.Reflection.FieldInfo[] fieldInfo = type.GetFields();
        foreach (System.Reflection.FieldInfo info in fieldInfo)
        {
            switch (info.FieldType.ToString())
            {
            case "System.String":
                string resS;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resS);
                    info.SetValue(data, resS);
                }
                break;

            case "System.Single":
                float resF;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resF);
                    info.SetValue(data, resF);
                }
                break;

            case "System.Int32":
                int resI;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resI);
                    info.SetValue(data, resI);
                }
                break;

            case "System.Boolean":
                bool resB;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resB);
                    info.SetValue(data, resB);
                }
                break;

            case "UnityEngine.Vector3":
                Vector3 resV;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resV);
                    info.SetValue(data, resV);
                }
                break;

            case "UnityEngine.Quaternion":
                Quaternion resQ;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resQ);
                    info.SetValue(data, resQ);
                }
                break;

            case "UnityEngine.Color":
                Color resC;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resC);
                    info.SetValue(data, resC);
                }
                break;

            case "UnityEngine.Rect":
                Rect resR;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resR);
                    info.SetValue(data, resR);
                }
                break;

            case "UnityEngine.Vector2":
                Vector2 resV2;
                if (stream.Has(info.Name))
                {
                    stream.Content(info.Name, out resV2);
                    info.SetValue(data, resV2);
                }
                break;

            default:
                if (stream.Has(info.Name))
                {
                    if (info.FieldType.IsEnum) // Enum
                    {
                        string e;
                        stream.Content(info.Name, out e);
                        info.SetValue(data, Enum.Parse(info.FieldType, e));
                    }
                    else if (info.FieldType.IsGenericType) // can only be a List
                    {
                        Type       containedType = info.FieldType.GetGenericArguments()[0];
                        Type       typeList      = typeof(List <>);
                        Type       actualType    = typeList.MakeGenericType(containedType);
                        MethodInfo addMethod     = actualType.GetMethod("Add");
                        object     list          = Activator.CreateInstance(actualType);
                        stream.Start(info.Name).List("item", delegate(XMLInStream stream2){
                            object o = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[] { o });
                        }).End();
                        info.SetValue(data, list);
                    }
                    else if (info.FieldType.IsArray) // Array
                    {
                        Type       containedType = info.FieldType.GetElementType();
                        Type       typeList      = typeof(List <>);
                        Type       actualType    = typeList.MakeGenericType(containedType);
                        MethodInfo addMethod     = actualType.GetMethod("Add");
                        MethodInfo toArrayMethod = actualType.GetMethod("ToArray");
                        object     list          = Activator.CreateInstance(actualType);
                        stream.Start(info.Name).List("item", delegate(XMLInStream stream2){
                            object o = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[] { o });
                        }).End();
                        object array = toArrayMethod.Invoke(list, new object[] {});
                        info.SetValue(data, array);
                    }
                    else // Embedded Object
                    {
                        stream.Start(info.Name);
                        object created = DeserializeObject(stream, info.FieldType);
                        stream.End();
                        info.SetValue(data, created);
                    }
                }
                break;
            }
        }
        return(data);
    }
コード例 #2
0
    private static object DeserializeObject(XMLInStream stream, Type type)
    {
        //IL_01ec: Unknown result type (might be due to invalid IL or missing references)
        //IL_021f: Unknown result type (might be due to invalid IL or missing references)
        //IL_0252: Unknown result type (might be due to invalid IL or missing references)
        //IL_0285: Unknown result type (might be due to invalid IL or missing references)
        //IL_02b8: Unknown result type (might be due to invalid IL or missing references)
        object obj = Activator.CreateInstance(type);

        FieldInfo[] fields = type.GetFields();
        FieldInfo[] array  = fields;
        Type        containedType;
        MethodInfo  addMethod;
        object      list;
        Type        containedType2;
        MethodInfo  addMethod2;
        object      list2;

        foreach (FieldInfo fieldInfo in array)
        {
            switch (fieldInfo.FieldType.ToString())
            {
            case "System.String":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out string value12);
                    fieldInfo.SetValue(obj, value12);
                }
                break;

            case "System.Single":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out float value7);
                    fieldInfo.SetValue(obj, value7);
                }
                break;

            case "System.Int32":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out int value9);
                    fieldInfo.SetValue(obj, value9);
                }
                break;

            case "System.Boolean":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out bool value5);
                    fieldInfo.SetValue(obj, value5);
                }
                break;

            case "UnityEngine.Vector3":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Vector3 value10);
                    fieldInfo.SetValue(obj, value10);
                }
                break;

            case "UnityEngine.Quaternion":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Quaternion value8);
                    fieldInfo.SetValue(obj, value8);
                }
                break;

            case "UnityEngine.Color":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Color value6);
                    fieldInfo.SetValue(obj, value6);
                }
                break;

            case "UnityEngine.Rect":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Rect value4);
                    fieldInfo.SetValue(obj, value4);
                }
                break;

            case "UnityEngine.Vector2":
                if (stream.Has(fieldInfo.Name))
                {
                    stream.Content(fieldInfo.Name, out Vector2 value11);
                    fieldInfo.SetValue(obj, value11);
                }
                break;

            default:
                if (stream.Has(fieldInfo.Name))
                {
                    if (fieldInfo.FieldType.IsEnum)
                    {
                        stream.Content(fieldInfo.Name, out string value);
                        fieldInfo.SetValue(obj, Enum.Parse(fieldInfo.FieldType, value));
                    }
                    else if (fieldInfo.FieldType.IsGenericType)
                    {
                        containedType = fieldInfo.FieldType.GetGenericArguments()[0];
                        Type typeFromHandle = typeof(List <>);
                        Type type2          = typeFromHandle.MakeGenericType(containedType);
                        addMethod = type2.GetMethod("Add");
                        list      = Activator.CreateInstance(type2);
                        stream.Start(fieldInfo.Name).List("item", delegate(XMLInStream stream2)
                        {
                            object obj3 = DeserializeListElement(stream2, containedType);
                            addMethod.Invoke(list, new object[1]
                            {
                                obj3
                            });
                        }).End();
                        fieldInfo.SetValue(obj, list);
                    }
                    else if (fieldInfo.FieldType.IsArray)
                    {
                        containedType2 = fieldInfo.FieldType.GetElementType();
                        Type typeFromHandle2 = typeof(List <>);
                        Type type3           = typeFromHandle2.MakeGenericType(containedType2);
                        addMethod2 = type3.GetMethod("Add");
                        MethodInfo method = type3.GetMethod("ToArray");
                        list2 = Activator.CreateInstance(type3);
                        stream.Start(fieldInfo.Name).List("item", delegate(XMLInStream stream2)
                        {
                            object obj2 = DeserializeListElement(stream2, containedType2);
                            addMethod2.Invoke(list2, new object[1]
                            {
                                obj2
                            });
                        }).End();
                        object value2 = method.Invoke(list2, new object[0]);
                        fieldInfo.SetValue(obj, value2);
                    }
                    else
                    {
                        stream.Start(fieldInfo.Name);
                        object value3 = DeserializeObject(stream, fieldInfo.FieldType);
                        stream.End();
                        fieldInfo.SetValue(obj, value3);
                    }
                }
                break;
            }
        }
        return(obj);
    }