Example #1
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            if (obj == null)
            {
                SerializerFactory.GetSerializer(null).WriteObject(null, outStream);
                return;
            }
            Type     type     = obj.GetType();
            TypeInfo typeInfo = GetTypeInfo(type);
            var      typId    = typeInfo.TypeId.GetBytes();

            outStream.Write(typId, 0, 4);
            if (outStream.WriteRef(obj))
            {
                return;
            }
            foreach (var p in typeInfo.Properies)
            {
                var value = p.GetValue(obj, null);
                if (value == null)
                {
                    SerializerFactory.GetSerializer(null).WriteObject(value, outStream);
                }
                else
                {
                    outStream.WriteUInt32(value.GetType().GetTypeId());
                    if (value is IGaeaSerializer)
                    {
                        ((IGaeaSerializer)value).Serialize(outStream);
                    }
                    else
                    {
                        try
                        {
                            SerializerFactory.GetSerializer(value.GetType()).WriteObject(value, outStream);
                        }
                        catch (Exception err)
                        {
                            var e = new SerializeException(err);
                            e.Type         = type;
                            e.PropertyName = p.Name;
                            throw e;
                        }
                    }
                }
            }
        }
Example #2
0
        public static Type GetRealGenType(Type type, Type defType)
        {
            Type t = GetRealGenType(defType);

            if (t != null)
            {
                return(t);
            }
            if (type.IsGenericType)
            {
                if (defType.IsGenericType)
                {
                    return(type.MakeGenericType(defType.GetGenericArguments()));
                }
                else
                {
                    var err = new SerializeException("defType must be generic type.");
                    err.Type = type;
                    throw err;
                }
            }
            return(type);
        }
Example #3
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);
        }