コード例 #1
0
ファイル: ByteSerializer.cs プロジェクト: sinshu/dtf
        /// <summary>
        /// 配列aのindexの位置にobjectの内容を書き込む。
        /// aは十分に確保してあると仮定している。
        /// </summary>
        /// <param name="a"></param>
        /// <param name="index"></param>
        /// <param name="o"></param>
        public static void Write(byte[] a, int index, object o)
        {
            Type t = o.GetType();

            if (t == typeof(short))
            {
                MemoryCopy.SetShort(a, index, (short)o);
            }
            else if (t == typeof(int))
            {
                MemoryCopy.SetInt(a, index, (int)o);
            }
            else if (t == typeof(string))
            {
                string s = o as string;
                MemoryCopy.SetInt(a, index, s.Length);
                MemoryCopy.SetString(a, index + sizeof(int), s, s.Length);
            }
            else if (t == typeof(byte[]))
            {
                byte[] a2 = o as byte[];
                MemoryCopy.SetInt(a, index, a2.Length);
                MemoryCopy.MemCopy(a, index + sizeof(int), a2, 0, a2.Length);
            }

            throw new ByteSerializerException();             // サポート外
        }
コード例 #2
0
ファイル: ByteSerializer.cs プロジェクト: sinshu/dtf
        public static object Read(byte[] a, ref int index, Type t)
        {
            if (t == typeof(short))
            {
                short s = MemoryCopy.GetShort(a, index);
                index += sizeof(short);
                return((object)s);
            }
            else if (t == typeof(int))
            {
                int i = MemoryCopy.GetInt(a, index);
                index += sizeof(int);
                return((object)i);
            }
            else if (t == typeof(string))
            {
                string s;
                int    length;
                length = MemoryCopy.GetInt(a, index);
                index += sizeof(int);
                s      = MemoryCopy.GetString(a, index, length);
                index += length * 2;
                return((object)s);
            }
            else if (t == typeof(byte[]))
            {
                int length;
                length = MemoryCopy.GetInt(a, index);
                index += sizeof(int);
                byte[] a2 = new byte[length];
                MemoryCopy.MemCopy(a2, index, a, index, length);
                index += length;
                return((object)a2);
            }

            throw new ByteSerializerException();             // サポート外
        }