Esempio n. 1
0
 /// <summary>
 /// 反序列化基元类型数据
 /// </summary>
 private static object DeserializePrimitive(FGReader reader, Type type)
 {
     if (type.Equals(typeof(int)))
     {
         return(reader.ReadInt());
     }
     else if (type.Equals(typeof(byte)))
     {
         return(reader.ReadByte());
     }
     else if (type.Equals(typeof(sbyte)))
     {
         return(reader.ReadSByte());
     }
     else if (type.Equals(typeof(float)))
     {
         return(reader.ReadFloat());
     }
     else if (type.Equals(typeof(long)))
     {
         return(reader.ReadLong());
     }
     else if (type.Equals(typeof(uint)))
     {
         return(reader.ReadUint());
     }
     return(null);
 }
Esempio n. 2
0
        private static object Deserialize(FGReader reader, Type type)
        {
            // 基本类型
            if (type.IsPrimitive)
            {
                return(DeserializePrimitive(reader, type));
            }
            else if (type.Equals(typeof(string)))
            {
                return(reader.ReadString());
            }

            /* 自定义特性类反序列化 */
            //Dictionary<string, int> lenDict = new Dictionary<string, int>(); //保存 数组长度标志 和 数组 的关系
            object       ob         = Activator.CreateInstance(type, true);
            FGReflection reflection = new FGReflection(type);

            foreach (var item in reflection.fieldList)
            {
                FieldInfo info = item.info;
                Type      t    = info.FieldType;

                if (t.Equals(typeof(string)))
                {
                    int length = reader.ReadInt();
                    if (length > 0)
                    {
                        info.SetValue(ob, reader.ReadString(length));
                    }
                    else
                    {
                        info.SetValue(ob, reader.ReadString());
                    }
                }
                else
                {
                    // 数组
                    if (t.IsArray)
                    {
                        //根据长度记录,序列化
                        int length = reader.ReadInt();
                        if (length > 0)
                        {
                            Type at = t.GetElementType();
                            if (at.Equals(typeof(byte)) || at.Equals(typeof(sbyte)))
                            {
                                byte[] array = reader.ReadBytes(length);
                                info.SetValue(ob, array);
                            }
                            else
                            {
                                Array array = Array.CreateInstance(t.GetElementType(), length);
                                for (int i = 0; i < length; i++)
                                {
                                    array.SetValue(Deserialize(reader, at), i);
                                }
                                info.SetValue(ob, array);
                            }
                        }
                        else
                        {
                            HelperTool.LogError("FGSerializer.Deserialize : 反序列化" + type.Name + "失败");
                            continue;
                        }
                    }
                    // 泛型List
                    else if (t.IsGenericType)
                    {
                        Type   listType = t.GetGenericArguments()[0];
                        Type[] types    = t.GetInterfaces();
                        if (Array.IndexOf(types, typeof(IEnumerable)) > -1)
                        {
                            int        length = reader.ReadInt();
                            Type       lsType = typeof(List <>);
                            var        makeme = lsType.MakeGenericType(listType);
                            object     o      = Activator.CreateInstance(makeme);
                            MethodInfo method = o.GetType().GetMethod("Add");
                            object[]   insts  = new object[length];
                            for (int i = 0; i < length; i++)
                            {
                                Object inst = Deserialize(reader, listType);
                                method.Invoke(o, new object[] { inst });
                            }
                            info.SetValue(ob, o);
                        }
                    }
                    else
                    {
                        //非数组结构直接序列化,如果字段为数组的长度标志,记录下来
                        info.SetValue(ob, DeserializePrimitive(reader, t));
                    }
                }
            }
            return(ob);
        }
Esempio n. 3
0
        public static T Deserialize <T>(MemoryStream stream)
        {
            FGReader reader = new FGReader(stream);

            return((T)Deserialize(reader, typeof(T)));
        }