public T Load <T>(string contextName, Func <Stream, T> loader)
 {
     using (var stream = PostProcessorHost?.LoadContextInfo())
     {
         if (stream == null)
         {
             return(default(T));
         }
         var deserializer = new StreamDeserializer(stream);
         var seg          = deserializer.ReadSegment();
         var entries      = deserializer.ReadDictionaryLazy(seg);
         if (!entries.TryGetValue(contextName, out Lazy <object> lazy))
         {
             return(default(T));
         }
         var bytes = (byte[])lazy.Value;
         return(loader(new MemoryStream(bytes)));
     }
 }
Beispiel #2
0
        public void TestDictionaryLazy()
        {
            var ms       = new MemoryStream();
            var ss       = new StreamSerializer(ms);
            var expected = new Dictionary <string, object>
            {
                ["a"]    = 1,
                ["bcd"]  = "efg",
                ["hijk"] = null,
            };

            ss.Write(expected);
            ms.Position = 0;
            var sd     = new StreamDeserializer(ms);
            var actual = sd.ReadDictionaryLazy(sd.ReadSegment());

            Assert.Equal(expected["a"], actual["a"].Value);
            Assert.Equal(expected["bcd"], actual["bcd"].Value);
            Assert.Equal(expected["hijk"], actual["hijk"].Value);
        }