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);
            }
        }
        //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);
                }
            }
        }
示例#3
0
        public MySerializerObject()
        {
            //var members = typeof(T).GetDataMembers(true, false, true, true, false, true);
            var members = typeof(T).GetDataMembers(true, true, true, true, false, true, true, true);
            var filter  = members
                          .Where(s => !Attribute.IsDefined(s, typeof(NoSerializeAttribute)))
                          .Where(s => Attribute.IsDefined(s, typeof(SerializeAttribute)) || s.IsMemberPublic())
                          .Where(Filter);

            m_memberSerializers = filter.Select(s => MyFactory.CreateMemberSerializer <T>(s)).ToArray();
        }
示例#4
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;
        }
示例#5
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;
        }
示例#6
0
 public static void Write <T>(BitStream stream, ref T value, MySerializeInfo serializeInfo = null)
 {
     MySerializationHelpers.Write(stream, ref value, MyFactory.GetSerializer <T>(), serializeInfo ?? MySerializeInfo.Default);
 }
示例#7
0
 public static void CreateAndRead <T>(BitStream stream, out T value, MySerializeInfo serializeInfo = null)
 {
     MySerializationHelpers.CreateAndRead(stream, out value, MyFactory.GetSerializer <T>(), serializeInfo ?? MySerializeInfo.Default);
 }