示例#1
0
            private static IDictionary UnpackDictionary <TKey, TValue>(byte[] content)
            {
                int numberOfPairs;
                var markerLess = RemoveMarker(content, out numberOfPairs);

                var output = new Dictionary <TKey, TValue>();

                if (numberOfPairs == 0)
                {
                    return(output);
                }

                var packed = PackStream.GetPackedEntities(markerLess);

                for (var i = 0; i < packed.Length; i += 2)
                {
                    var  key   = PackStream.Unpack(packed[i]);
                    var  value = PackStream.Unpack(packed[i + 1]);
                    Type genericType;
                    if (List.IsEnumerable(typeof(TValue), out genericType))
                    {
                        var method        = typeof(List).GetTypeInfo().GetDeclaredMethod("Unpack");
                        var genericMethod = method.MakeGenericMethod(genericType);
                        var list          = genericMethod.Invoke(null, new object[] { packed[i + 1].Original }) as IEnumerable;
                        value = (TValue)list;
                    }

                    output.Add(key, value);
                }

                return(output);
            }
示例#2
0
            public static IEnumerable <T> Unpack <T>(byte[] content)
            {
                if (!IsUnpackable(content))
                {
                    throw new ArgumentException("Not a list.", nameof(content));
                }

                var numberOfElements = GetNumberOfElements(content);
                var bytesToSkip      = GetSizeOfMarkerInBytes(numberOfElements);

                var unpacked = PackStream.Unpack(content.Skip(bytesToSkip).ToArray());

                return(unpacked.Cast <T>());
            }
            public void UnpacksSuccessMetaDataProperlyAsString()
            {
                var bytes    = new byte[] { 0xA1, 0x86, 0x66, 0x69, 0x65, 0x6C, 0x64, 0x73, 0x91, 0x81, 0x78 };
                var expected = new Dictionary <string, IEnumerable <string> > {
                    { "fields", new List <string> {
                          "x"
                      } }
                };

                var actual = PackStream.Unpack <Dictionary <string, IEnumerable <string> > >(bytes);

                foreach (var kvp in expected)
                {
                    actual.Keys.Should().Contain(kvp.Key);
                    foreach (var val in kvp.Value)
                    {
                        actual[kvp.Key].Should().Contain(val);
                    }
                }
            }