private static KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > GenerateSerializationInformations(Type serializationType) { string header = serializationType.GetCustomAttribute <PacketHeaderAttribute>()?.Identification; if (string.IsNullOrEmpty(header)) { throw new Exception($"Packet header cannot be empty. PacketType: {serializationType.Name}"); } Dictionary <PacketIndexAttribute, PropertyInfo> packetsForPacketDefinition = new Dictionary <PacketIndexAttribute, PropertyInfo>(); foreach (PropertyInfo packetBasePropertyInfo in serializationType.GetProperties().Where(x => x.GetCustomAttributes(false).OfType <PacketIndexAttribute>().Any())) { PacketIndexAttribute indexAttribute = packetBasePropertyInfo.GetCustomAttributes(false).OfType <PacketIndexAttribute>().FirstOrDefault(); if (indexAttribute != null) { packetsForPacketDefinition.Add(indexAttribute, packetBasePropertyInfo); } } // order by index var keyValuePairs = packetsForPacketDefinition.OrderBy(p => p.Key.Index); KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > serializationInformatin = new KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> >(new Tuple <Type, string>(serializationType, header), packetsForPacketDefinition); _packetSerializationInformations.Add(serializationInformatin.Key, serializationInformatin.Value); return(serializationInformatin); }
private static void GenerateSerializationInformations <TPacketBase>() where TPacketBase : PacketBase { _packetSerializationInformations = new Dictionary <Tuple <Type, String>, Dictionary <PacketIndexAttribute, PropertyInfo> >(); //Iterate thru all PacketBase implementations foreach (Type packetBaseType in typeof(TPacketBase).Assembly.GetTypes().Where(p => !p.IsInterface && typeof(TPacketBase).BaseType.IsAssignableFrom(p))) { string header = packetBaseType.GetCustomAttribute <HeaderAttribute>()?.Identification; Dictionary <PacketIndexAttribute, PropertyInfo> PacketsForPacketDefinition = new Dictionary <PacketIndexAttribute, PropertyInfo>(); foreach (PropertyInfo packetBasePropertyInfo in packetBaseType.GetProperties().Where(x => x.GetCustomAttributes(false).OfType <PacketIndexAttribute>().Any())) { PacketIndexAttribute indexAttribute = packetBasePropertyInfo.GetCustomAttributes(false).OfType <PacketIndexAttribute>().FirstOrDefault(); if (indexAttribute != null) { PacketsForPacketDefinition.Add(indexAttribute, packetBasePropertyInfo); } } //order by index PacketsForPacketDefinition.OrderBy(p => p.Key.Index); //add to serialization informations _packetSerializationInformations.Add(new Tuple <Type, String>(packetBaseType, header), PacketsForPacketDefinition); } }
private static string DeserializeValue(Type propertyType, object value, PacketIndexAttribute packetIndexAttribute = null) { if (propertyType != null) { // check for nullable without value or string if (propertyType.Equals(typeof(string)) && String.IsNullOrEmpty(Convert.ToString(value))) { return(" -"); } if (Nullable.GetUnderlyingType(propertyType) != null && String.IsNullOrEmpty(Convert.ToString(value))) { return(" -1"); } // enum should be casted to number if (propertyType.BaseType != null && propertyType.BaseType.Equals(typeof(Enum))) { return(String.Format(" {0}", Convert.ToInt16(value))); } else if (propertyType.Equals(typeof(bool))) { // bool is 0 or 1 not True or False return(Convert.ToBoolean(value) ? " 1" : " 0"); } else if (propertyType.BaseType.Equals(typeof(PacketDefinition))) { var subpacketSerializationInfo = GetSerializationInformation(propertyType); return(DeserializeSubpacket(value, subpacketSerializationInfo, packetIndexAttribute?.IsReturnPacket ?? false, packetIndexAttribute?.RemoveSeparator ?? false)); } else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)) && propertyType.GenericTypeArguments[0].BaseType.Equals(typeof(PacketDefinition))) { return(DeserializeSubpackets((IList)value, propertyType, packetIndexAttribute?.RemoveSeparator ?? false)); } else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>))) //simple list { return(DeserializeSimpleList((IList)value, propertyType)); } else { return(String.Format(" {0}", value)); } } return(String.Empty); }
private static object DeserializeValue(Type packetPropertyType, string currentValue, PacketIndexAttribute packetIndexAttribute, MatchCollection packetMatches, bool includesKeepAliveIdentity = false) { // check for empty value and cast it to null if (currentValue == "-1" || currentValue == "-") { currentValue = null; } // enum should be casted to number if (packetPropertyType.BaseType != null && packetPropertyType.BaseType.Equals(typeof(Enum))) { object convertedValue = null; try { if (currentValue != null && packetPropertyType.IsEnumDefined(Enum.Parse(packetPropertyType, currentValue))) { convertedValue = Enum.Parse(packetPropertyType, currentValue); } } catch (Exception) { Logger.Log.Warn($"Could not convert value {currentValue} to type {packetPropertyType.Name}"); } return(convertedValue); } if (packetPropertyType.Equals(typeof(bool))) // handle boolean values { return(currentValue != "0"); } if (packetPropertyType.BaseType != null && packetPropertyType.BaseType.Equals(typeof(PacketDefinition))) // subpacket { var subpacketSerializationInfo = GetSerializationInformation(packetPropertyType); return(DeserializeSubpacket(currentValue, packetPropertyType, subpacketSerializationInfo, packetIndexAttribute?.IsReturnPacket ?? false)); } if (packetPropertyType.IsGenericType && packetPropertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)) && // subpacket list packetPropertyType.GenericTypeArguments[0].BaseType.Equals(typeof(PacketDefinition))) { return(DeserializeSubpackets(currentValue, packetPropertyType, packetIndexAttribute?.RemoveSeparator ?? false, packetMatches, packetIndexAttribute?.Index, includesKeepAliveIdentity)); } if (packetPropertyType.IsGenericType && packetPropertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>))) // simple list { return(DeserializeSimpleList(currentValue, packetPropertyType)); } if (Nullable.GetUnderlyingType(packetPropertyType) != null && string.IsNullOrEmpty(currentValue)) // empty nullable value { return(null); } if (Nullable.GetUnderlyingType(packetPropertyType) != null) // nullable value { if (packetPropertyType.GenericTypeArguments[0]?.BaseType.Equals(typeof(Enum)) == true) { return(Enum.Parse(packetPropertyType.GenericTypeArguments[0], currentValue)); } return(Convert.ChangeType(currentValue, packetPropertyType.GenericTypeArguments[0])); } return(Convert.ChangeType(currentValue, packetPropertyType)); // cast to specified type }