internal PacketDeserializeSetup(byte[] data, object instance) { IMemberAccessor memberAccessor = DynamicMethodMemberAccessor.FindClassAccessor(instance.GetType()); this._bytesArr.AddRange(data); this.StartDeserialize(instance, memberAccessor); this._bytesArr.Clear(); }
private void StartSerialize(object instance, IMemberAccessor memberAccessor) { var properties = instance.GetType().GetProperties(); foreach (System.Reflection.PropertyInfo property in properties) { var type = property.PropertyType; var value = memberAccessor.GetValue(instance, property.Name); //property.GetValue(instance, null); if (value == null) { this.WriteInt32(-1); continue; } if (type.Equals(typeof(Boolean))) { this.WriteByte((Boolean)value ? (Byte)1 : (Byte)0); } else if (type.Equals(typeof(Boolean[]))) { this.WriteArrayBool((Boolean[])value); } else if (type.Equals(typeof(Byte))) { this.WriteByte((Byte)value); } else if (type.Equals(typeof(Byte[]))) { this.WriteArrayByte((Byte[])value); } else if (type.Equals(typeof(Int16))) { this.WriteInt16((Int16)value); } else if (type.Equals(typeof(Int16[]))) { this.WriteArray((Int16[])value, WriteInt16); } else if (type.Equals(typeof(Int32))) { this.WriteInt32((Int32)value); } else if (type.Equals(typeof(Int32[]))) { this.WriteArray((Int32[])value, WriteInt32); } else if (type.Equals(typeof(Int64))) { this.WriteInt64((Int64)value); } else if (type.Equals(typeof(Int64[]))) { this.WriteArray((Int64[])value, WriteInt64); } else if (type.Equals(typeof(String))) { this.WriteString((String)value); } else if (type.Equals(typeof(String[]))) { this.WriteArray((String[])value, WriteString); } else if (type.Equals(typeof(Single))) { this.WriteFloat((Single)value); } else if (type.Equals(typeof(Single[]))) { this.WriteArray((Single[])value, WriteFloat); } else if (type.Equals(typeof(Double))) { this.WriteDouble((Double)value); } else if (type.Equals(typeof(Double[]))) { this.WriteArray((Double[])value, WriteDouble); } else if (type.Equals(typeof(DateTime))) { this.WriteDateTime((DateTime)value); } else if (type.BaseType.Equals(typeof(Enum))) { this.WriteEnum(value); } else if (type.IsArray) //暂时仅支持数组 { if (type.GetElementType().IsValueType) { throw new Exception("not supported this value array!"); } var elementType = type.GetElementType(); var arrayMemberAccessor = memberAccessor.Type.Equals(elementType) ? memberAccessor : DynamicMethodMemberAccessor.FindClassAccessor(elementType); this.WriteArray((Array)value, arrayMemberAccessor); } else if (type.IsClass) { var childerInstanceMemberAccessor = memberAccessor.Type.Equals(property.PropertyType) ? memberAccessor : DynamicMethodMemberAccessor.FindClassAccessor(property.PropertyType); this.WriteInt32(1);//是否为空标志位 this.StartSerialize(value, childerInstanceMemberAccessor); } else { throw new Exception("not supported this type:" + type.FullName); } } }
internal PacketSerializeSetup(object instance) { IMemberAccessor memberAccessor = DynamicMethodMemberAccessor.FindClassAccessor(instance.GetType()); this.StartSerialize(instance, memberAccessor); }
private void StartDeserialize(object instance, IMemberAccessor memberAccessor) { var properties = instance.GetType().GetProperties(); foreach (PropertyInfo property in properties) { var type = property.PropertyType; if (type.Equals(typeof(Boolean))) { memberAccessor.SetValue(instance, property.Name, this.ReadBoolean()); } else if (type.Equals(typeof(Boolean[]))) { memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadBoolean)); } else if (type.Equals(typeof(Byte))) { memberAccessor.SetValue(instance, property.Name, this.ReadByte()); } else if (type.Equals(typeof(Byte[]))) { memberAccessor.SetValue(instance, property.Name, this.ReadBytes()); } else if (type.Equals(typeof(Int16))) { memberAccessor.SetValue(instance, property.Name, this.ReadInt16()); } else if (type.Equals(typeof(Int16[]))) { memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadInt16)); } else if (type.Equals(typeof(Int32))) { memberAccessor.SetValue(instance, property.Name, this.ReadInt32()); } else if (type.Equals(typeof(Int32[]))) { memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadInt32)); } else if (type.Equals(typeof(Int64))) { memberAccessor.SetValue(instance, property.Name, this.ReadInt64()); } else if (type.Equals(typeof(Int64[]))) { memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadInt64)); } else if (type.Equals(typeof(String))) { memberAccessor.SetValue(instance, property.Name, this.ReadString()); } else if (type.Equals(typeof(String[]))) { memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadString)); } else if (type.Equals(typeof(Single))) { memberAccessor.SetValue(instance, property.Name, this.ReadFloat()); } else if (type.Equals(typeof(Single[]))) { memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadFloat)); } else if (type.Equals(typeof(Double))) { memberAccessor.SetValue(instance, property.Name, this.ReadDouble()); } else if (type.Equals(typeof(Double[]))) { memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadDouble)); } else if (type.Equals(typeof(DateTime))) { memberAccessor.SetValue(instance, property.Name, this.ReadDateTime()); } else if (type.BaseType.Equals(typeof(Enum))) { memberAccessor.SetValue(instance, property.Name, this.ReadEnum(property.PropertyType)); } else if (type.IsArray) { if (type.GetElementType().IsValueType) { throw new Exception("not supported this value array!"); } var elementType = type.GetElementType(); var arrayMemberAccessor = memberAccessor.Type.Equals(elementType) ? memberAccessor : DynamicMethodMemberAccessor.FindClassAccessor(elementType); memberAccessor.SetValue(instance, property.Name, this.ReadArray(property.PropertyType, arrayMemberAccessor)); } else { if (this.ReadInt32() == 1) { var childerInstanceMemberAccessor = memberAccessor.Type.Equals(property.PropertyType) ? memberAccessor : DynamicMethodMemberAccessor.FindClassAccessor(property.PropertyType); var childerInstance = Activator.CreateInstance(property.PropertyType); this.StartDeserialize(childerInstance, childerInstanceMemberAccessor); childerInstanceMemberAccessor.SetValue(instance, property.Name, childerInstance); } } } }