public static void BytesToField(ISerializablePacket Packet, byte[] Bytes, string FieldName) { if (Bytes == null || Bytes.Length <= 0) return; FieldInfo Info = Packet.GetType().GetField(FieldName); ISerializableFieldAttribute[] FieldsAttr = Info.GetCustomAttributes(typeof(ISerializableFieldAttribute), true) as ISerializableFieldAttribute[]; if (FieldsAttr != null && FieldsAttr.Length > 0) { ISerializableField Field = Activator.CreateInstance(FieldsAttr[0].GetSerializableType()) as ISerializableField; Field.Index = FieldsAttr[0].Index; Field.val = Info.GetValue(Packet); Field.PacketType = PacketProcessor.GetFieldType(Field); PacketInStream Str = new PacketInStream(Bytes, Bytes.Length); Field.Deserialize(ref Str); Field.ApplyToFieldInfo(Info, Packet, Info.FieldType); } else Info.SetValue(Packet, null); }
public static byte[] FieldToBytes(ISerializablePacket Packet, string FieldName) { FieldInfo Info = Packet.GetType().GetField(FieldName); ISerializableFieldAttribute[] FieldsAttr = Info.GetCustomAttributes(typeof(ISerializableFieldAttribute), true) as ISerializableFieldAttribute[]; if (FieldsAttr != null && FieldsAttr.Length > 0) { ISerializableField Field = Activator.CreateInstance(FieldsAttr[0].GetSerializableType()) as ISerializableField; Field.Index = FieldsAttr[0].Index; Field.val = Info.GetValue(Packet); Field.PacketType = PacketProcessor.GetFieldType(Field); PacketOutStream Str = new PacketOutStream(); Field.Serialize(ref Str,true); byte[] Result = Str.ToArray(); return Result; } else return null; }