Пример #1
0
        /// <summary>
        /// Get key value pairs from serialized byte array
        /// </summary>
        /// <param name="bytes">byte array</param>
        /// <returns>key value pairs</returns>
        public static KeyValuePair <string, object>[] GetKeyValuePairs(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return(null);
            }

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                using (BinaryReader reader = new BinaryReader(ms))
                {
                    Int32Column  intCol = new Int32Column();
                    StringColumn strCol = new StringColumn();

                    int length = intCol.Read(reader);
                    KeyValuePair <string, object>[] result = new KeyValuePair <string, object> [length];

                    for (int index = 0; index < length; index++)
                    {
                        string key   = strCol.Read(reader);
                        string value = strCol.Read(reader);
                        result[index] = new KeyValuePair <string, object>(key, value);
                    }

                    return(result);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Serializes key value pairs into byte array
        /// </summary>
        /// <param name="array">array of key value pairs</param>
        /// <returns>byte array</returns>
        public static byte[] GetBytes(KeyValuePair <string, object>[] array)
        {
            if (array == null || array.Length == 0)
            {
                return(null);
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    Int32Column  intCol = new Int32Column();
                    StringColumn strCol = new StringColumn();

                    intCol.Write(writer, array.Length);
                    foreach (KeyValuePair <string, object> pair in array)
                    {
                        strCol.Write(writer, pair.Key);
                        strCol.Write(writer, pair.Value != null ? pair.Value.ToString() : string.Empty);
                    }
                }

                ms.Flush();
                return(ms.ToArray());
            }
        }