Esempio n. 1
0
        /// <summary>
        /// 添加项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="typeID">分配的ID</param>
        /// <param name="readInfo">读取器</param>
        private static void AddItem <T>(int typeID, ReadInfo readInfo, WriteInfo writeInfo)
        {
            MemTypeItem item = new MemTypeItem(typeID, typeof(T), readInfo, writeInfo);

            _dicMemTypeItem[typeID] = item;
            _dicMemTypeItemByFullName[item.ItemType.FullName] = item;
        }
Esempio n. 2
0
        private static bool InitTypes()
        {
            _dicMemTypeItem           = new Dictionary <int, MemTypeItem>();
            _dicMemTypeItemByFullName = new Dictionary <string, MemTypeItem>();
            MemTypeItem item = null;

            AddItem <bool>(1, ReadBoolean, WriteBoolean);

            AddItem <short>(2, ReadInt16, WriteInt16);
            AddItem <int>(3, ReadInt, WriteInt);
            AddItem <long>(4, ReadInt64, WriteInt64);

            AddItem <ushort>(5, ReadUInt16, WriteUInt16);
            AddItem <uint>(6, ReadUInt, WriteUInt);
            AddItem <ulong>(7, ReadUInt64, WriteUInt64);

            AddItem <byte>(8, ReadByte, WriteByte);
            AddItem <byte[]>(9, ReadBytes, WriteBytes);
            AddItem <char>(10, ReadChar, WriteChar);
            AddItem <char[]>(11, ReadChars, WriteChars);
            AddItem <decimal>(12, ReadDecimal, WriteDecimal);
            AddItem <double>(13, ReadDouble, WriteDouble);
            AddItem <sbyte>(14, ReadSByte, WriteSByte);
            AddItem <float>(15, ReadSingle, WriteSingle);

            AddItem <string>(16, ReadString, WriteString);

            AddItem <DateTime>(17, ReadDateTime, WriteDateTime);
            AddItem <TimeSpan>(18, ReadTimeSpan, WriteTimeSpan);
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// 根据类型ID获取类型信息
        /// </summary>
        /// <param name="typeID">类型ID</param>
        /// <returns></returns>
        public static MemTypeItem GetTypeByID(int typeID)
        {
            MemTypeItem item = null;

            if (_dicMemTypeItem.TryGetValue(typeID, out item))
            {
                return(item);
            }
            return(null);
        }
Esempio n. 4
0
        /// <summary>
        /// 获取类型信息
        /// </summary>
        /// <param name="objType"></param>
        /// <returns></returns>
        public static MemTypeItem GetTypeInfo(Type objType)
        {
            string      key  = objType.FullName;
            MemTypeItem item = null;

            if (_dicMemTypeItemByFullName.TryGetValue(key, out item))
            {
                return(item);
            }
            return(null);
        }
Esempio n. 5
0
        /// <summary>
        /// 写入数据表信息
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="bw"></param>
        private static void WriteDataTable(DataTable dt, BinaryWriter bw)
        {
            MemTypeManager.WriteString(bw, dt.TableName);

            //写入列数
            MemTypeManager.WriteInt(bw, dt.Columns.Count);
            List <MemTypeItem> lstItem = new List <MemTypeItem>(dt.Columns.Count);//列的信息
            MemTypeItem        item    = null;

            //写入列信息
            foreach (DataColumn col in dt.Columns)
            {
                MemTypeManager.WriteString(bw, col.ColumnName);//列名

                //列类型ID
                item = MemTypeManager.GetTypeInfo(col.DataType);
                if (item != null)
                {
                    MemTypeManager.WriteInt(bw, item.TypeID);
                    lstItem.Add(item);
                }
                else
                {
                    lstItem.Add(item);
                }
            }

            //行数
            MemTypeManager.WriteInt(bw, dt.Rows.Count);

            //写入数据

            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < lstItem.Count; i++)
                {
                    if (row.IsNull(i))
                    {
                        lstItem[i].WriterHandle(bw, null);
                        continue;
                    }
                    object value = row[i];
                    if (lstItem[i] == null)
                    {
                        continue;
                    }
                    lstItem[i].WriterHandle(bw, value);
                }
            }
        }
Esempio n. 6
0
        private static DataTable ReadDataTable(BinaryReader br)
        {
            DataTable dt = new DataTable();

            dt.TableName = MemTypeManager.ReadString(br) as string;

            int                columnCount = (int)MemTypeManager.ReadInt(br);//列数
            string             name        = null;
            int                typeCode    = 0;
            List <MemTypeItem> lstItem     = new List <MemTypeItem>(columnCount);//列的信息

            for (int i = 0; i < columnCount; i++)
            {
                name     = MemTypeManager.ReadString(br) as string;
                typeCode = (int)MemTypeManager.ReadInt(br);
                MemTypeItem item = MemTypeManager.GetTypeByID(typeCode);
                if (item != null)
                {
                    dt.Columns.Add(name, item.ItemType);
                    lstItem.Add(item);
                }
            }
            int rows = (int)MemTypeManager.ReadInt(br);

            dt.BeginLoadData();
            for (int i = 0; i < rows; i++)
            {
                DataRow dr = dt.NewRow();
                for (int k = 0; k < lstItem.Count; k++)
                {
                    MemTypeItem colItem = lstItem[k];
                    object      value   = colItem.ReadHandle(br);
                    if (value != null)
                    {
                        dr[k] = value;
                    }
                }
                dt.Rows.Add(dr);
            }
            dt.EndLoadData();
            return(dt);
        }