Ejemplo n.º 1
0
        //public static void Read<TMember>(BitStream stream, ref TMember value, MySerializer<TMember> serializer, MySerializeInfo info)
        //{
        //    if (info.IsNullable)
        //        throw new InvalidOperationException("Read does not support nullable");

        //    if (MySerializer<TMember>.IsClass && info.IsDynamic)
        //    {
        //        MyFactory.GetSerializer(value.GetType()).Read(stream, value, info);
        //    }
        //    else
        //    {
        //        serializer.Read(stream, ref value, info);
        //    }
        //}

        public static void Write <TMember>(BitStream stream, ref TMember value, MySerializer <TMember> serializer, MySerializeInfo info)
        {
            if (WriteNullable(stream, ref value, info.IsNullable, serializer))
            {
                if (MySerializer <TMember> .IsClass && info.IsDynamic)
                {
                    var memberType = typeof(TMember);
                    var valueType  = value.GetType();

                    bool writeType = true;

                    if (info.IsDynamicDefault)
                    {
                        writeType = memberType != valueType;
                        stream.WriteBool(writeType);
                    }

                    if (writeType)
                    {
                        stream.WriteDynamicType(memberType, valueType, info.DynamicSerializer);
                    }
                    MyFactory.GetSerializer(value.GetType()).Write(stream, value, info);
                }
                else if (MySerializer <TMember> .IsValueType || value.GetType() == typeof(TMember))
                {
                    serializer.Write(stream, ref value, info);
                }
                else
                {
                    throw new MySerializeException(MySerializeErrorEnum.DynamicNotAllowed);
                }
            }
        }
Ejemplo n.º 2
0
        public sealed override void Init(MemberInfo memberInfo, MySerializeInfo info)
        {
            if (m_serializer != null)
            {
                throw new InvalidOperationException("Already initialized");
            }

            m_getter     = memberInfo.CreateGetterRef <TOwner, TMember>();
            m_setter     = memberInfo.CreateSetterRef <TOwner, TMember>();
            m_serializer = MyFactory.GetSerializer <TMember>();
            m_info       = info;
            m_memberInfo = memberInfo;
        }
Ejemplo n.º 3
0
        public sealed override void Init(MemberInfo memberInfo, MySerializeInfo info)
        {
            if (m_serializer != null)
            {
                throw new InvalidOperationException("Already initialized");
            }

#if !XB1 // XB1_SYNC_SERIALIZER_NOEMIT
            m_getter = memberInfo.CreateGetterRef <TOwner, TMember>();
            m_setter = memberInfo.CreateSetterRef <TOwner, TMember>();
#endif // !XB1
            m_serializer = MyFactory.GetSerializer <TMember>();
            m_info       = info;
            m_memberInfo = memberInfo;
        }
Ejemplo n.º 4
0
        static bool WriteNullable <T>(BitStream stream, ref T value, bool isNullable, MySerializer <T> serializer)
        {
            if (isNullable)
            {
                T    def      = default(T);
                bool hasValue = !serializer.Equals(ref value, ref def);
                stream.WriteBool(hasValue);
                return(hasValue);
            }
            else
            {
                if (!typeof(T).IsValueType && value == null)
                {
                    throw new MySerializeException(MySerializeErrorEnum.NullNotAllowed);
                }

                return(true);
            }
        }
Ejemplo n.º 5
0
        public static bool CreateAndRead <TMember>(BitStream stream, out TMember result, MySerializer <TMember> serializer, MySerializeInfo info)
        {
            if (ReadNullable(stream, info.IsNullable))
            {
                if (MySerializer <TMember> .IsClass && info.IsDynamic)
                {
                    Type type     = typeof(TMember);
                    bool readType = true;

                    if (info.IsDynamicDefault)
                    {
                        readType = stream.ReadBool();
                    }

                    if (readType)
                    {
                        type = stream.ReadDynamicType(typeof(TMember), info.DynamicSerializer);
                    }

                    object value;
                    MyFactory.GetSerializer(type).Read(stream, out value, info);
                    result = (TMember)value;
                }
                else
                {
                    serializer.Read(stream, out result, info);
                }
                return(true);
            }
            else
            {
                result = default(TMember);
                return(false);
            }
        }