Пример #1
0
        private IPacket DeserializeIPacket(TypeCreator dic, string packetContent, bool includesKeepAliveIdentity, bool hasHeader)
        {
            var deg     = (IPacket)dic.Constructor.DynamicInvoke();
            var matches = Regex.Matches(packetContent, @"([^(\s\v)]+[\.]+[(\s\v)]?)+((?=(\s\v))|$)|([^(\s\v)]+)((?=\s)|$)").OfType <Match>()
                          .ToArray();

            if (matches.Length > 0 && dic.packetDeserializerDictionary.Count > 0)
            {
                var maxindex  = dic.packetDeserializerDictionary.Max(s => s.Key.Item2.Index);
                var trueIndex = -1;
                foreach (var packetBasePropertyInfo in dic.packetDeserializerDictionary)
                {
                    var isMaxIndex     = packetBasePropertyInfo.Key.Item2.Index == maxindex;
                    var keepaliveIndex = includesKeepAliveIdentity ? 1 : 0;
                    var currentIndex   = packetBasePropertyInfo.Key.Item2.Index + (hasHeader ? 1 : 0) + keepaliveIndex;
                    trueIndex = trueIndex == -1 ? currentIndex : trueIndex;
                    if (currentIndex < matches.Length)
                    {
                        var value = DeserializeValue(packetBasePropertyInfo.Key, packetBasePropertyInfo.Key.Item1,
                                                     matches, ref trueIndex, isMaxIndex);
                        foreach (var validation in packetBasePropertyInfo.Key.Item4)
                        {
                            var validate = validation.GetValidationResult(value, new ValidationContext(deg)
                            {
                                MemberName = packetBasePropertyInfo.Key.Item3,
                            });

                            deg.ValidationResult = deg.ValidationResult ?? validate;
                        }

                        if (packetBasePropertyInfo.Key.Item1.IsEnum && !Enum.IsDefined(packetBasePropertyInfo.Key.Item1, value))
                        {
                            deg.ValidationResult = new ValidationResult("Invalid Enum value",
                                                                        new[] { packetBasePropertyInfo.Key.Item3 });
                        }

                        if (deg.ValidationResult?.ErrorMessage.Length > 0)
                        {
                            deg.IsValid = false;
                        }

                        packetBasePropertyInfo.Value.DynamicInvoke(deg, value);
                    }
                    else if (isMaxIndex && packetBasePropertyInfo.Key.Item1 == typeof(string))
                    {
                        packetBasePropertyInfo.Value.DynamicInvoke(deg, string.Empty);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(deg);
        }