コード例 #1
0
            private SerializedList DeserializeList(BinaryReader reader)
            {
                var value = new SerializedList(DeserializeDefaults(reader));
                var count = reader.ReadInt32();

                for (int i = 0; i < count; i++)
                {
                    var mode = (ESerializableMode)reader.ReadInt32();
                    switch (mode)
                    {
                    case ESerializableMode.Primitive:
                        value.Add(DeserializePrimitive(reader));
                        break;

                    case ESerializableMode.Enum:
                        value.Add(DeserializeEnum(reader));
                        break;

                    case ESerializableMode.List:
                        value.Add(DeserializeList(reader));
                        break;

                    case ESerializableMode.Class:
                        value.Add(DeserializeClass(reader));
                        break;

                    default:
                        break;
                    }
                }

                return(value);
            }
コード例 #2
0
            private void WriteList(BinaryWriter writer, SerializedList obj)
            {
                WriteDefaults(writer, obj);
                writer.Write(obj.Values.Count);

                foreach (var item in obj.Values)
                {
                    writer.Write((int)item.Mode);
                    switch (item.Mode)
                    {
                    case ESerializableMode.Primitive:
                        WritePrimitive(writer, (SerializedPrimitive)item);
                        break;

                    case ESerializableMode.Enum:
                        WriteEnum(writer, (SerializedEnum)item);
                        break;

                    case ESerializableMode.List:
                        WriteList(writer, (SerializedList)item);
                        break;

                    case ESerializableMode.Class:
                        WriteClass(writer, (SerializedClass)item);
                        break;

                    default:
                        break;
                    }
                }
            }
コード例 #3
0
        private object ReadList(SerializedList value)
        {
            var   type     = Type.GetType(value.Type);
            IList instance = null;

            if (type.IsArray())
            {
                instance = Array.CreateInstance(type.GetElementType() ?? typeof(object), value.Values.Count);

                for (int i = 0; i < value.Values.Count; i++)
                {
                    var item = value.Values[i];
                    instance[i] = Read(item);
                }
            }
            else
            {
                instance = (IList)Activator.CreateInstance(type);

                foreach (var item in value.Values)
                {
                    instance.Add(Read(item));
                }
            }

            return(instance);
        }
コード例 #4
0
        private SerializedList SerializeList(object value, Type valueType)
        {
            if (value == null)
            {
                return(new SerializedList(GetNextID(), valueType.AssemblyQualifiedName)
                {
                    IsNull = true
                });
            }

            var list = new SerializedList(GetNextID(), valueType.AssemblyQualifiedName);

            if (Compare(value, valueType, ref list.ID))
            {
                list.IsReference = true;
                return(list);
            }
            else
            {
                AddToComparables(list, value, valueType);
            }

            var  vList  = (IList)value;
            var  vCount = vList.Count;
            Type vType  = null;

            if (valueType.IsArray())
            {
                vType = valueType.GetElementType();
                if (valueType == typeof(Array))
                {
                    for (int i = 0; i < vList.Count; i++)
                    {
                        var item = vList[i];
                        if (item != null)
                        {
                            vType = item.GetType();
                            break;
                        }
                    }
                }
                if (vType == null)
                {
                    vType = typeof(object);
                }
            }
            else
            {
                vType = valueType.GetGenericArguments()[0];
            }

            Func <object, Type, SerializedBase> method = null;
            bool isClass = false;

            if (vType.IsArray())
            {
                method = SerializeList;
            }
            else if (vType.IsEnum)
            {
                method = SerializeEnum;
            }
            else if (vType.IsValueType && !vType.IsPrimitive)
            {
                if (vType == typeof(decimal))
                {
                    method = SerializePrimitive;
                }
                else
                {
                    method  = SerializeClass;
                    isClass = true;
                }
            }
            else if (vType.IsValueType)
            {
                method = SerializePrimitive;
            }
            else if (vType.IsClass)
            {
                if (vType == typeof(string))
                {
                    method = SerializePrimitive;
                }
                else if (vType.IsList())
                {
                    method = SerializeList;
                }
                else
                {
                    method  = SerializeClass;
                    isClass = true;
                }
            }

            for (int i = 0; i < vCount; i++)
            {
                if (isClass)
                {
                    var v = vList[i];
                    if (v == null)
                    {
                        list.Add(method(v, vType));
                    }
                    else
                    {
                        list.Add(method(v, v.GetType()));
                    }
                }
                else
                {
                    list.Add(method(vList[i], vType));
                }
            }

            return(list);
        }