Exemple #1
0
        /// <summary>
        /// 将对象序列化成字节数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected virtual byte[] SerializeObject(object value)
        {
            //all length
            //, item 1 name size, item 1 name data, item 1 value size, item 1 value data
            //, item 2 name size,  item 2 name data, item 2 value size, item 2 value data
            //, item N name size,  item N name data, item N value size, item N value data
            //
            var    ps = PropertyAccessor.GetGets(value.GetType());
            object itemValue;

            //
            byte[] nameBuffer, valueBuffer, sizeBuffer;
            int    length   = 0;
            int    psLength = ps.Length;

            using (var m = new MemoryStream())
            {
                //all length
                sizeBuffer = BaseDataConverter.ToBytes(psLength);
                m.Write(sizeBuffer, 0, SIZE_INT32);
                //
                for (int i = 0; i < psLength; i++)
                {
                    //name
                    nameBuffer = Encoding.UTF8.GetBytes(ps[i].PropertyInfo.Name);
                    length     = nameBuffer.Length;
                    sizeBuffer = BaseDataConverter.ToBytes(length);

                    m.Write(sizeBuffer, 0, SIZE_INT32);
                    m.Write(nameBuffer, 0, length);

                    //value
                    itemValue   = PropertyAccessor.GetValue(value, ps[i].PropertyInfo.Name);
                    valueBuffer = this.Serialize(itemValue);
                    if (valueBuffer == null)
                    {
                        sizeBuffer = BaseDataConverter.ToBytes(-1);
                        m.Write(sizeBuffer, 0, SIZE_INT32);
                        //no body
                    }
                    else
                    {
                        length     = valueBuffer.Length;
                        sizeBuffer = BaseDataConverter.ToBytes(length);
                        m.Write(sizeBuffer, 0, SIZE_INT32);
                        m.Write(valueBuffer, 0, length);
                    }
                }

                return(m.ToArray());
            }
        }
Exemple #2
0
        /// <summary>
        /// SerializeArray
        /// </summary>
        /// <param name="array"></param>
        private byte[] SerializeArray(object array)
        {
            //item length,    item 1, item 2, item N
            if (array is string[])
            {
                var    stringArray = (string[])array;
                byte[] itemBuffer, lengthBuffer, sizeBuffer;
                int    length;
                using (var m = new MemoryStream())
                {
                    //item length,  item size1, item 1, item size 2, item 2, item size N, item N

                    //write item length
                    lengthBuffer = BaseDataConverter.ToBytes(stringArray.Length);
                    m.Write(lengthBuffer, 0, SIZE_INT32);
                    //
                    foreach (string item in stringArray)
                    {
                        if (item == null)
                        {
                            sizeBuffer = BaseDataConverter.ToBytes(-1);
                            //write size
                            m.Write(sizeBuffer, 0, SIZE_INT32);
                            //null item no body
                        }
                        else if ("".Equals(item))
                        {
                            sizeBuffer = BaseDataConverter.ToBytes(0);
                            //write size
                            m.Write(sizeBuffer, 0, SIZE_INT32);
                            //empty item no body
                        }
                        else
                        {
                            itemBuffer = Encoding.UTF8.GetBytes(item);
                            length     = itemBuffer.Length;
                            sizeBuffer = BaseDataConverter.ToBytes(length);
                            //write size
                            m.Write(sizeBuffer, 0, SIZE_INT32);
                            //item
                            m.Write(itemBuffer, 0, length);
                        }
                    }
                    return(m.ToArray());
                }
            }

            if (array is DateTime[])
            {
                var workArray   = (DateTime[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_DATETIME];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    //BaseDataConverter.ToBytes(workArray[i].ToUniversalTime().Ticks, buffer, offset);
                    BaseDataConverter.ToBytes(workArray[i].Ticks, buffer, offset);
                    offset += SIZE_INT64;
                }
                return(buffer);
            }

            if (array is Boolean[])
            {
                var workArray   = (Boolean[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                for (int i = 0; i < arrayLength; i++)
                {
                    buffer[SIZE_INT32 + i] = (byte)(workArray[i] ? 1 : 0);
                }
                return(buffer);
            }

            if (array is Byte[])
            {
                var workArray   = (Byte[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                Array.Copy(workArray, 0, buffer, SIZE_INT32, arrayLength);
                return(buffer);
            }

            if (array is SByte[])
            {
                var workArray   = (SByte[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                for (int i = 0; i < arrayLength; i++)
                {
                    buffer[SIZE_INT32 + i] = (byte)workArray[i];
                }
                return(buffer);
            }

            if (array is Int16[])
            {
                var workArray   = (Int16[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_INT16];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_INT16;
                }
                return(buffer);
            }

            if (array is UInt16[])
            {
                var workArray   = (UInt16[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_UINT16];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_UINT16;
                }
                return(buffer);
            }

            if (array is Int32[])
            {
                var workArray   = (Int32[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_INT32];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_INT32;
                }
                return(buffer);
            }

            if (array is UInt32)
            {
                var workArray   = (UInt32[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_UINT32];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_UINT32;
                }
                return(buffer);
            }

            if (array is Int64[])
            {
                var workArray   = (Int64[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_INT64];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_INT64;
                }
                return(buffer);
            }

            if (array is UInt64[])
            {
                var workArray   = (UInt64[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_UINT64];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_UINT64;
                }
                return(buffer);
            }

            if (array is Char[])
            {
                var workArray   = (Char[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_CHAR];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_CHAR;
                }
                return(buffer);
            }

            if (array is Double[])
            {
                var workArray   = (Double[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_DOUBLE];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_DOUBLE;
                }
                return(buffer);
            }

            if (array is Single[])
            {
                var workArray   = (Single[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_SINGLE];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_SINGLE;
                }
                return(buffer);
            }

            if (array is Decimal[])
            {
                var workArray   = (Decimal[])array;
                var arrayLength = workArray.Length;
                var buffer      = new byte[SIZE_INT32 + arrayLength * SIZE_DECIMAL];
                BaseDataConverter.ToBytes(arrayLength, buffer, 0);
                var offset = SIZE_INT32;
                for (int i = 0; i < arrayLength; i++)
                {
                    BaseDataConverter.ToBytes(workArray[i], buffer, offset);
                    offset += SIZE_DECIMAL;
                }
                return(buffer);
            }
            else
            {
                byte[] itemBuffer, lengthBuffer, sizeBuffer;
                var    workArray   = (Array)array;
                var    arrayLength = workArray.Length;
                int    length;
                using (var m = new MemoryStream())
                {
                    //write length
                    lengthBuffer = BaseDataConverter.ToBytes(arrayLength);
                    m.Write(lengthBuffer, 0, SIZE_INT32);

                    //item length,  item size1, item 1, item size 2, item 2, item size N, item N
                    foreach (var item in workArray)
                    {
                        if (item == null || DBNull.Value.Equals(item))
                        {
                            sizeBuffer = BaseDataConverter.ToBytes(-1);
                            //write size
                            m.Write(sizeBuffer, 0, SIZE_INT32);
                            //null item no body
                        }
                        else if (item.GetType().IsArray)
                        {
                            itemBuffer = this.SerializeArray(item);
                            length     = itemBuffer.Length;
                            //write size
                            sizeBuffer = BaseDataConverter.ToBytes(length);
                            m.Write(sizeBuffer, 0, SIZE_INT32);
                            //item
                            m.Write(itemBuffer, 0, length);
                        }
                        else
                        {
                            itemBuffer = SerializeObject(item);
                            length     = itemBuffer.Length;
                            //write size
                            sizeBuffer = BaseDataConverter.ToBytes(length);
                            m.Write(sizeBuffer, 0, SIZE_INT32);
                            //item
                            m.Write(itemBuffer, 0, length);
                        }
                    }
                    return(m.ToArray());
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 序列化数据
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public byte[] Serialize(object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (DBNull.Value.Equals(value))
            {
                return(null);
            }

            if (value is string)
            {
                return(Encoding.UTF8.GetBytes((string)value));
            }


            if (value is DateTime)
            {
                //return BaseDataConverter.ToBytes(((DateTime)value).ToUniversalTime().Ticks);
                return(BaseDataConverter.ToBytes(((DateTime)value).Ticks));
            }

            if (value is Boolean)
            {
                var buffer = new byte[1];
                buffer[0] = (byte)((Boolean)value ? 1 : 0);
                return(buffer);
            }

            if (value is Byte)
            {
                var buffer = new byte[1];
                buffer[0] = (byte)value;
                return(buffer);
            }

            if (value is SByte)
            {
                var buffer = new byte[1];
                buffer[0] = (byte)((sbyte)value);
                return(buffer);
            }

            if (value is Int16)
            {
                return(BaseDataConverter.ToBytes((Int16)value));
            }

            if (value is UInt16)
            {
                return(BaseDataConverter.ToBytes((UInt16)value));
            }

            if (value is Int32)
            {
                return(BaseDataConverter.ToBytes((Int32)value));
            }

            if (value is UInt32)
            {
                return(BaseDataConverter.ToBytes((UInt32)value));
            }

            if (value is Int64)
            {
                return(BaseDataConverter.ToBytes((Int64)value));
            }

            if (value is UInt64)
            {
                return(BaseDataConverter.ToBytes((UInt64)value));
            }

            if (value is Char)
            {
                return(BaseDataConverter.ToBytes((Char)value));
            }

            if (value is Double)
            {
                return(BaseDataConverter.ToBytes((Double)value));
            }

            if (value is Single)
            {
                return(BaseDataConverter.ToBytes((Single)value));
            }

            if (value is Decimal)
            {
                return(BaseDataConverter.ToBytes((Decimal)value));
            }

            if (value is Array)
            {
                return(this.SerializeArray(value));
            }

            return(this.SerializeObject(value));
        }