示例#1
0
        private static string SerializeValue(Type propertyType, object value, PacketIndexAttribute packetIndexAttribute = null)
        {
            if (propertyType == null)
            {
                return(string.Empty);
            }

            // check for nullable without value or string
            if (propertyType == 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 == typeof(Enum))
            {
                return($" {Convert.ToInt16(value)}");
            }

            if (propertyType == typeof(bool))
            {
                // bool is 0 or 1 not True or False
                return(Convert.ToBoolean(value) ? " 1" : " 0");
            }

            if (propertyType.BaseType != null && propertyType.BaseType == typeof(PacketDefinition))
            {
                KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > subpacketSerializationInfo = GetSerializationInformation(propertyType);
                return(SerializeSubpacket(value, subpacketSerializationInfo, packetIndexAttribute?.IsReturnPacket ?? false, packetIndexAttribute?.RemoveSeparator ?? false));
            }

            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)) &&
                propertyType.GenericTypeArguments[0].BaseType == typeof(PacketDefinition))
            {
                return(SerializeSubpackets((IList)value, propertyType, packetIndexAttribute?.RemoveSeparator ?? false));
            }

            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>))) //simple list
            {
                return(SerializeSimpleList((IList)value, propertyType));
            }

            return($" {value}");
        }
示例#2
0
        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
            IOrderedEnumerable <KeyValuePair <PacketIndexAttribute, PropertyInfo> > 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);

            if (_packetSerializationInformations == null)
            {
                _packetSerializationInformations = new Dictionary <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> >();
            }

            _packetSerializationInformations.Add(serializationInformatin.Key, serializationInformatin.Value);

            return(serializationInformatin);
        }
示例#3
0
        private static object DeserializeValue(Type packetPropertyType, string currentValue, PacketIndexAttribute packetIndexAttribute, MatchCollection packetMatches)
        {
            // enum should be casted to number
            if (packetPropertyType.BaseType != null && packetPropertyType.BaseType == typeof(Enum))
            {
                object convertedValue = null;
                try
                {
                    if (currentValue != null && packetPropertyType.IsEnumDefined(Enum.Parse(packetPropertyType, currentValue)))
                    {
                        convertedValue = Enum.Parse(packetPropertyType, currentValue);
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine($"Could not convert value {currentValue} to type {packetPropertyType.Name}");
                }

                return(convertedValue);
            }

            if (packetPropertyType == typeof(bool)) // handle boolean values
            {
                return(currentValue != "0");
            }

            if (packetPropertyType.BaseType != null && packetPropertyType.BaseType == typeof(PacketDefinition)) // subpacket
            {
                KeyValuePair <Tuple <Type, string>, Dictionary <PacketIndexAttribute, PropertyInfo> > 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 == typeof(PacketDefinition))
            {
                return(DeserializeSubpackets(currentValue, packetPropertyType, packetIndexAttribute?.RemoveSeparator ?? false, packetMatches, packetIndexAttribute?.Index));
            }

            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 == typeof(Enum))
                {
                    return(Enum.Parse(packetPropertyType.GenericTypeArguments[0], currentValue));
                }

                return(Convert.ChangeType(currentValue, packetPropertyType.GenericTypeArguments[0]));
            }

            return(Convert.ChangeType(currentValue, packetPropertyType)); // cast to specified type
        }