示例#1
0
 public override object ReadObject(GaeaStream inStream, Type defType)
 {
     byte[] head = new byte[5];
     inStream.SafeRead(head, 0, 5);
     int isRef = head.ToByte(0);
     int hashcode = head.ToInt32(1);
     if (isRef > 0)
     {
         object o = inStream.GetRef(hashcode);
         if (o == null)
             return string.Empty;
         if (!(o is string))
             throw new Exception("Type error!");
         return o;
     }
     int len = inStream.ReadInt32();
     if (len == 0)
     {
         inStream.SetRef(hashcode, string.Empty);
         return string.Empty;
     }
     byte[] buffer = new byte[len];
     inStream.SafeRead(buffer, 0, len);
     string str = inStream.Encoder.GetString(buffer);
     inStream.SetRef(hashcode, str);
     return str;
 }
示例#2
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            var typeId = inStream.ReadUInt32();
            if (typeId == 0)
                return null;
            byte isRef = (byte)inStream.ReadByte();
            int hashcode = inStream.ReadInt32();
            if (isRef > 0)
            {
                return inStream.GetRef(hashcode);
            }
            var type = defType;
            var keyTypeId = inStream.ReadUInt32();
            var keyType = keyTypeId.ToType();
            object key = SerializerFactory.GetSerializer(keyType).ReadObject(inStream,defType.GetGenericArguments()[0]);

            object value = null;
            var valueTypeId = inStream.ReadUInt32();
            var valueType = valueTypeId.ToType();
            if (valueTypeId != 0)
            {
                value = SerializerFactory.GetSerializer(valueType).ReadObject(inStream, defType.GetGenericArguments()[1]);
            }
            object obj = Activator.CreateInstance(type, key, value);
            inStream.SetRef(hashcode, obj);
            return obj;
        }
示例#3
0
 public override object ReadObject(GaeaStream inStream,Type defType)
 {
     uint typeId = inStream.ReadUInt32();
     if (typeId == 0)
         return null;
     byte isRef = (byte)inStream.ReadByte();
     int hashCode = inStream.ReadInt32();
     if (isRef > 0)
     {
         return inStream.GetRef(hashCode);
     }
     int len = inStream.ReadInt32();
     Type type = typeId.ToType();
     if (type == typeof(byte))
     {
         byte[] buffer = new byte[len];
         inStream.SafeRead(buffer, 0, len);
         return buffer;
     }
     if (type.IsGenericType)
     {
         type = GenericHelper.GetRealGenType(type, defType.GetElementType());
     }
     var array = Array.CreateInstance(type, len);
     if (!type.IsPrimitive)
     {
         for (int i = 0; i < len; i++)
         {
             var itemTypeId = inStream.ReadUInt32();
             if (itemTypeId == 0)
             {
                 array.SetValue(null, i);
             }
             else
             {
                 Type itemType = itemTypeId.ToType();
                 var value = SerializerFactory.GetSerializer(itemType).ReadObject(inStream, defType.GetElementType());
                 array.SetValue(value, i);
             }
         }
     }
     else
     {
         for (int i = 0; i < len; i++)
         {
             var value = SerializerFactory.GetSerializer(type).ReadObject(inStream,defType.GetElementType());
             array.SetValue(value, i);
         }
     }
     inStream.SetRef(hashCode, array);
     return array;
 }
示例#4
0
 public override object ReadObject(GaeaStream inStream,Type defType)
 {
     uint typeId = inStream.ReadUInt32();
     if (typeId == 0)
         return null;
     Type type = typeId.ToType();
     if (type == null)
     {
         throw new NotFindTypeException("Not find the type from current application.type:" + defType.ToString() + ".typeId:" + typeId);
     }
     if (type.IsGenericType)
         type = type.MakeGenericType(defType.GetGenericArguments());
     int fbyte = inStream.ReadByte();
     int hashcode = inStream.ReadInt32();
     if (fbyte > 0)
     {
         return inStream.GetRef(hashcode);
     }
     TypeInfo typeInfo = GetTypeInfo(type);
     object obj = Activator.CreateInstance(type, true);
     foreach (var p in typeInfo.Properies)
     {
         var ptypeId = inStream.ReadUInt32();
         if (ptypeId == 0)
         {
             p.SetValue(obj, null, null);
             continue;
         }
         Type t = ptypeId.ToType();
         if (t == null)
         {
             throw new NotFindTypeException("Not find the type from current application.type:" + p.PropertyType.Name + ",propery name:" + p.Name);
         }
         if (t.GetInterface("IGaeaSerializer") != null)
         {
             var value = ((IGaeaSerializer)Activator.CreateInstance(t));
             value.Derialize(inStream);
             p.SetValue(obj, value, null);
         }
         else
         {
             try
             {
                 var value = SerializerFactory.GetSerializer(t).ReadObject(inStream, p.PropertyType);
                 object pValue = value;
                 if (p.PropertyType == typeof(byte))
                 {
                     pValue = Convert.ToByte(value);
                 }
                 else if (p.PropertyType == typeof(int))
                 {
                     pValue = Convert.ToInt32(value);
                 }
                 else if (p.PropertyType == typeof(long))
                 {
                     pValue = Convert.ToInt64(value);
                 }
                 p.SetValue(obj, pValue, null);
             }
             catch (Exception err)
             {
                 var e = new SerializeException(err);
                 e.Type = type;
                 e.PropertyName = p.Name;
                 throw e;
             }
         }
     }
     inStream.SetRef(hashcode, obj);
     return obj;
 }
示例#5
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            var typeId = inStream.ReadUInt32();
            if (typeId == 0)
                return null;
            byte isRef = (byte)inStream.ReadByte();
            int hashcode = inStream.ReadInt32();
            IList obj;
            if (isRef > 0)
            {
                obj = (IList)inStream.GetRef(hashcode);
            }
            else
            {
                var type = typeId.ToType();
                if (type == null)
                {
                    throw new TypeNoMatchException("Type id can't convert to type !typeId:" + typeId);
                }
                if (type != typeof(IList))
                {
                    throw new TypeNoMatchException("Type must be IList!type:" + type.FullName);
                }
                if (!defType.IsAbstract && !defType.IsInterface && typeof(IList).IsAssignableFrom(defType))
                {
                    type = defType;
                }
                else
                {
                    if (defType.IsGenericType)
                    {
                        var gtypes = defType.GetGenericArguments();
                        if (gtypes.Length != 1)
                        {
                            throw new TypeNoMatchException("Defind type Generic parameters length error!deftype:" + defType.FullName);
                        }
                        type = typeof(List<>).MakeGenericType(gtypes);
                    }
                    else
                    {
                        type = typeof(ArrayList);
                    }
                    if (!defType.IsAssignableFrom(type))
                    {
                        throw new TypeNoMatchException("Defind type and value type not match !defind type:" + defType.FullName + ",value type:" + type.FullName); 
                    }
                }               
                var len = inStream.ReadInt32();
                obj = (IList)Activator.CreateInstance(type, true);
                for (int i = 0; i < len; i++)
                {
                    var itemTypeId = inStream.ReadUInt32();
                    if (itemTypeId == 0)
                    {
                        obj.Add(null);

                    }
                    else
                    {
                        var itemType = itemTypeId.ToType();
                        if (itemType == null)
                        {
                            throw new NotFindTypeException("Not find the type from current application.typeId:" + itemTypeId);
                        }
                        Type dType = itemType;//item define type
                        if (type.IsGenericType)
                        {
                            dType = type.GetGenericArguments()[0];
                        }
                        object value = SerializerFactory.GetSerializer(dType).ReadObject(inStream, dType);
                        obj.Add(value);
                    }
                }
                inStream.SetRef(hashcode, obj);
            }

            return obj;
        }
示例#6
0
 public override object ReadObject(GaeaStream inStream,Type defType)
 {
     var typeId = inStream.ReadUInt32();
     if (typeId == 0)
         return null;
     byte isRef = (byte)inStream.ReadByte();
     int hashcode = inStream.ReadInt32();
     if (isRef > 0)
     {
         return inStream.GetRef(hashcode);
     }
     var type = typeId.ToType();
     if (type == null)
     {
         throw new NotFindTypeException("Type id can't convert to type !typeId:" + typeId);
     }
     if (type != typeof(IDictionary))
     {
         throw new TypeNoMatchException("Type must be IDictionary!type:" + type.FullName);
     }
     if (!defType.IsAbstract && !defType.IsInterface && typeof(IDictionary).IsAssignableFrom(defType))
     {
         type = defType;
     }
     else
     {
         if (defType.IsGenericType)
         {
             type = typeof(Dictionary<,>).MakeGenericType(defType.GetGenericArguments());
         }
         else
         {
             type = typeof(Hashtable);
         }
         if (!defType.IsAssignableFrom(type))
         {
             throw new TypeNoMatchException("Defind type and value type not match !defind type:" + defType.FullName + ",value type:" + type.FullName);
         }
     }
     var len = inStream.ReadInt32();
     IDictionary obj = (IDictionary)Activator.CreateInstance(type, true);
     Type[] gtypes = null;
     if (type.IsGenericType)
     {
         gtypes = type.GetGenericArguments();
     }
     for (int i = 0; i < len; i++)
     {
         var keyTypeId = inStream.ReadUInt32();
         var keyType = keyTypeId.ToType();
         var dkeyType = keyType;
         if (type.IsGenericType)
         {
             dkeyType = gtypes[0];
         }
         object key = SerializerFactory.GetSerializer(keyTypeId.ToType()).ReadObject(inStream, dkeyType);
         byte[] valueTypeBuffer = new byte[4];
         inStream.SafeRead(valueTypeBuffer, 0, 4);
         var valueTypeId = valueTypeBuffer.ToUInt32(0);
         if (valueTypeId == 0)
         {
             obj[key] = null;
         }
         else
         {
             var valueType = valueTypeId.ToType();
             var dvalueType = valueType;
             if (type.IsGenericType)
                 dvalueType = gtypes[1];
             object value = SerializerFactory.GetSerializer(valueTypeId.ToType()).ReadObject(inStream, dvalueType);
             obj[key] = value;
         }
     }
     inStream.SetRef(hashcode, obj);
     return obj;
 }