Example #1
0
        public static void Serialize(MemoryStream stream, object ob)
        {
            if (stream == null || ob == null)
            {
                return;
            }

            FGWrite write = new FGWrite(stream);

            Serialize(write, ob, ob.GetType());
        }
Example #2
0
        /// <summary>
        /// 核心序列化代码
        /// </summary>
        private static void Serialize(FGWrite write, object ob, Type type)
        {
            // 基本类型
            if (type.IsPrimitive)
            {
                if (type.Equals(typeof(int)))
                {
                    write.Write((int)ob);
                }
                else if (type.Equals(typeof(float)))
                {
                    write.Write((float)ob);
                }
                else if (type.Equals(typeof(byte)))
                {
                    write.Write((byte)ob);
                }
                else if (type.Equals(typeof(long)))
                {
                    write.Write((long)ob);
                }
                else
                {
                    HelperTool.LogError("FGSerializer.Deserialize : 序列化类型不支持");
                }
                return;
            }

            // 字符串
            if (type.Equals(typeof(string)))
            {
                string str = (string)ob;
                write.Write(str.Length);
                write.WriteString(str);
                return;
            }

            // 数组
            if (type.IsArray)
            {
                Array arr = (Array)ob;
                if (arr != null)
                {
                    if (arr.Length > 0)
                    {
                        write.Write(arr.Length);
                        Type at = arr.GetValue(0).GetType();
                        if (at.Equals(typeof(byte)) || at.Equals(typeof(sbyte)))
                        {
                            write.Write((byte[])ob);
                        }
                        else
                        {
                            for (int i = 0; i < arr.Length; i++)
                            {
                                Serialize(write, arr.GetValue(i), at);
                            }
                        }
                    }
                }
                return;
            }

            // 泛型List
            if (type.IsGenericType)
            {
                Type   listType = type.GetGenericArguments()[0];
                Type[] types    = type.GetInterfaces();
                if (Array.IndexOf(types, typeof(IEnumerable)) > -1)
                {
                    IEnumerable en         = ob as IEnumerable;
                    var         enumerator = en.GetEnumerator();
                    int         length     = 0;
                    while (enumerator.MoveNext())
                    {
                        length++;
                    }
                    write.Write(length);
                    enumerator = en.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        Serialize(write, enumerator.Current, listType);
                    }
                }
                return;
            }

            // 类
            FGReflection reflection = new FGReflection(type);

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

                Serialize(write, info.GetValue(ob), t);
            }
        }