/// <summary> /// 尝试读取目标对象指定成员的值,处理基础类型、特殊类型、基础类型数组、特殊类型数组,通过委托方法处理成员 /// </summary> /// <remarks> /// 简单类型在value中返回,复杂类型直接填充target; /// </remarks> /// <param name="reader">读取器</param> /// <param name="target">目标对象</param> /// <param name="member">成员</param> /// <param name="type">成员类型,以哪一种类型读取</param> /// <param name="encodeInt">是否编码整数</param> /// <param name="allowNull">是否允许空</param> /// <param name="isProperty">是否处理属性</param> /// <param name="value">成员值</param> /// <param name="callback">处理成员的方法</param> /// <returns>是否读取成功</returns> protected virtual Boolean ReadMember(BinaryReaderX reader, Object target, MemberInfoX member, Type type, Boolean encodeInt, Boolean allowNull, Boolean isProperty, out Object value, BinaryReaderX.ReadCallback callback) { if (type == null) type = member.Type; // 接口支持 if (typeof(IBinaryAccessor).IsAssignableFrom(type)) { // 读取对象 value = member.GetValue(target); // 实例化对象 if (value == null) { //value = Activator.CreateInstance(member.Type); value = CreateInstance(type); //member.SetValue(target, value); } if (value == null) return false; // 调用接口 IBinaryAccessor accessor = value as IBinaryAccessor; accessor.Read(reader); return true; } return reader.TryReadObject(target, member, type, encodeInt, true, isProperty, out value, callback); }
private static Boolean ReadMember(BinaryReaderX reader, Object target, MemberInfoX member, Type type, Boolean encodeInt, Boolean allowNull, Boolean isProperty, out Object value, ReadCallback callback) { // 使用自己作为处理成员的方法 return reader.TryReadObject(target, member, type, encodeInt, allowNull, isProperty, out value, callback); }
/// <summary> /// 反序列化 /// </summary> /// <param name="stream"></param> /// <returns></returns> public static Message Deserialize(Stream stream) { BinaryReaderX reader = new BinaryReaderX(stream); // 读取了响应类型和消息类型后,动态创建消息对象 //Int32 id = reader.ReadEncodedInt32(); Int32 id = reader.ReadByte(); if (id <= 0) throw new InvalidDataException("无效的消息唯一编码" + id); Message msg = MessageHandler.CreateMessage(id); msg.Read(reader); if (id != msg.ID) throw new InvalidDataException("反序列化后的消息唯一编码不匹配。"); return msg; }
/// <summary> /// 尝试读取目标对象指定成员的值,处理基础类型、特殊类型、基础类型数组、特殊类型数组,通过委托方法处理成员 /// </summary> /// <remarks> /// 简单类型在value中返回,复杂类型直接填充target; /// </remarks> /// <param name="reader">读取器</param> /// <param name="target">目标对象</param> /// <param name="member">成员</param> /// <param name="type">成员类型,以哪一种类型读取</param> /// <param name="encodeInt">是否编码整数</param> /// <param name="allowNull">是否允许空</param> /// <param name="isProperty">是否处理属性</param> /// <param name="value">成员值</param> /// <param name="callback">处理成员的方法</param> /// <returns>是否读取成功</returns> protected override Boolean ReadMember(BinaryReaderX reader, object target, MemberInfoX member, Type type, bool encodeInt, bool allowNull, bool isProperty, out object value, BinaryReaderX.ReadCallback callback) { if (member.Member.Name != "_Entity") return base.ReadMember(reader, target, member, type, encodeInt, allowNull, isProperty, out value, callback); else return base.ReadMember(reader, target, member, EntityType, encodeInt, allowNull, isProperty, out value, callback); }
/// <summary> /// 从读取器中读取数据到对象 /// </summary> /// <param name="reader">读取器</param> public virtual void Read(BinaryReaderX reader) { //Read(this, reader, true, true, false); Object value = null; reader.TryReadObject(this, TypeX.Create(this.GetType()), null, true, false, false, out value, ReadMember); }