Пример #1
0
        /// <summary>
        /// Writes the Key+Value onto the stream and let the TBinaryAccessor handle how it's done.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="bw"></param>
        public static void Serialize(KeyValuePair <T, TX> input, BinaryWriter bw)
        {
            //Write the key onto the stream
            TBinaryAccessor <T> .Write(bw, input.Key);

            //Write the key onto the stream
            TBinaryAccessor <TX> .Write(bw, input.Value);
        }
 public static byte[] Serialize <T>(this IEnumerable <T> input)
 {
     using (MemoryStream ms = new MemoryStream())
         using (BinaryWriter bw = new BinaryWriter(ms))
         {
             input.ForEach(x => TBinaryAccessor <T> .Write(bw, x));
             return(ms.ToArray());
         }
 }
        public static List <T> DeSerialize <T>(this byte[] input)
        {
            var retVar = new List <T>();

            using (MemoryStream ms = new MemoryStream(input))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    while (br.BaseStream.Position != input.Length)
                    {
                        retVar.Add(TBinaryAccessor <T> .Read(br));
                    }
                }
            return(retVar);
        }
Пример #4
0
        public static Dictionary <T, TX> Deserialize(BinaryReader br)
        {
            //Read the amount of KeyValuePairs from the "header"
            var dictSize = br.ReadInt32();
            //Create a new instance of the Dictionary requested, use the constructor which accepts the dictSize to "boost" the performance a little
            var dstDict = new Dictionary <T, TX>(dictSize);

            for (int i = 0; i < dictSize; i++)
            {
                //Read the first value, set it as key and then read the second one and set it as value
                //As above, let the TBinaryAccessor handle how it's done.
                dstDict[TBinaryAccessor <T> .Read(br)] = TBinaryAccessor <TX> .Read(br);
            }
            return(dstDict);
        }