/// <summary> /// 解析指定类型数组对象 /// </summary> /// <param name="type"></param> /// <param name="buffer"></param> /// <returns></returns> private object DeserializeArray(Type type, byte[] buffer) { //legnth var arrayLength = BaseDataConverter.ToInt32(buffer); var offset = SIZE_INT32; var elementType = type.GetElementType(); // if (TYPE_STRING.Equals(elementType)) { //item length, item size1, item 1, item size 2, item 2, item size N, item N var stringArray = new string[arrayLength]; var itemSize = 0; for (int i = 0; i < arrayLength; i++) { itemSize = BaseDataConverter.ToInt32(buffer, offset); offset += SIZE_INT32; if (itemSize == -1) { stringArray[i] = null; } else if (itemSize == 0) { stringArray[i] = ""; } else { stringArray[i] = Encoding.UTF8.GetString(buffer, offset, itemSize); offset += itemSize; } } return(stringArray); } if (TYPE_DATETIME.Equals(elementType)) { var workArray = new DateTime[arrayLength]; for (int i = 0; i < arrayLength; i++) { //workArray[i] = new DateTime(BaseDataConverter.ToInt64(buffer, offset), DateTimeKind.Utc); workArray[i] = new DateTime(BaseDataConverter.ToInt64(buffer, offset)); offset += SIZE_DATETIME; } return(workArray); } if (TYPE_BOOLEAN.Equals(elementType)) { var workArray = new Boolean[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = buffer[offset] == 1; offset += SIZE_BOOLEAN; } return(workArray); } if (TYPE_BYTE.Equals(elementType)) { var workArray = new Byte[arrayLength]; Array.Copy(buffer, offset, workArray, 0, arrayLength); return(workArray); } if (TYPE_SBYTE.Equals(elementType)) { var workArray = new SByte[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = (SByte)buffer[offset]; offset += SIZE_SBYTE; } return(workArray); } if (TYPE_INT16.Equals(elementType)) { var workArray = new Int16[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToInt16(buffer, offset); offset += SIZE_INT16; } return(workArray); } if (TYPE_UINT16.Equals(elementType)) { var workArray = new UInt16[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToUInt16(buffer, offset); offset += SIZE_UINT16; } return(workArray); } if (TYPE_INT32.Equals(elementType)) { var workArray = new Int32[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToInt32(buffer, offset); offset += SIZE_INT32; } return(workArray); } if (TYPE_UINT32.Equals(elementType)) { var workArray = new UInt32[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToUInt32(buffer, offset); offset += SIZE_UINT32; } return(workArray); } if (TYPE_INT64.Equals(elementType)) { var workArray = new Int64[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToInt64(buffer, offset); offset += SIZE_INT64; } return(workArray); } if (TYPE_UINT64.Equals(elementType)) { var workArray = new UInt64[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToUInt64(buffer, offset); offset += SIZE_UINT64; } return(workArray); } if (TYPE_CHAR.Equals(elementType)) { var workArray = new Char[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToChar(buffer, offset); offset += SIZE_CHAR; } return(workArray); } if (TYPE_DOUBLE.Equals(elementType)) { var workArray = new Double[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToDouble(buffer, offset); offset += SIZE_DOUBLE; } return(workArray); } if (TYPE_SINGLE.Equals(elementType)) { var workArray = new Single[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToSingle(buffer, offset); offset += SIZE_SINGLE; } return(workArray); } if (TYPE_DECIMAL.Equals(elementType)) { var workArray = new Decimal[arrayLength]; for (int i = 0; i < arrayLength; i++) { workArray[i] = BaseDataConverter.ToDecimal(buffer, offset); offset += SIZE_DECIMAL; } return(workArray); } else { //item length, item size1, item 1, item size 2, item 2, item size N, item N var workArray = (IList)Array.CreateInstance(elementType, arrayLength); int itemSize; byte[] itemBuffer; for (int i = 0; i < arrayLength; i++) { itemSize = BaseDataConverter.ToInt32(buffer, offset); offset += SIZE_INT32; if (itemSize == -1) { workArray[i] = null; } else if (elementType.IsArray) { itemBuffer = new byte[itemSize]; Array.Copy(buffer, offset, itemBuffer, 0, itemSize); workArray[i] = this.DeserializeArray(elementType, itemBuffer); offset += itemSize; } else { itemBuffer = new byte[itemSize]; Array.Copy(buffer, offset, itemBuffer, 0, itemSize); workArray[i] = this.DeserializeObject(elementType, itemBuffer); offset += itemSize; } } return(workArray); } }
/// <summary> /// 返回指定类型的数据 /// </summary> /// <param name="type"></param> /// <param name="buffer"></param> /// <returns></returns> public object Deserialize(Type type, byte[] buffer) { //default if (buffer == null) { if (type.IsValueType) { return(System.Activator.CreateInstance(type)); } return(null); } if (TYPE_BOOLEAN.Equals(type)) { return(buffer[0] == 1); } if (TYPE_BYTE.Equals(type)) { return(buffer[0]); } if (TYPE_SBYTE.Equals(type)) { return((sbyte)buffer[0]); } if (TYPE_CHAR.Equals(type)) { return(BaseDataConverter.ToChar(buffer)); } if (TYPE_INT16.Equals(type)) { return(BaseDataConverter.ToInt16(buffer)); } if (TYPE_UINT16.Equals(type)) { return(BaseDataConverter.ToUInt16(buffer)); } if (TYPE_INT32.Equals(type)) { return(BaseDataConverter.ToInt32(buffer)); } if (TYPE_SINGLE.Equals(type)) { return(BaseDataConverter.ToSingle(buffer)); } if (TYPE_UINT32.Equals(type)) { return(BaseDataConverter.ToUInt32(buffer)); } if (TYPE_DATETIME.Equals(type)) { return(new DateTime(BaseDataConverter.ToInt64(buffer))); } //return new DateTime(BaseDataConverter.ToInt64(buffer), DateTimeKind.Utc); if (TYPE_DOUBLE.Equals(type)) { return(BaseDataConverter.ToDouble(buffer)); } if (TYPE_INT64.Equals(type)) { return(BaseDataConverter.ToInt64(buffer)); } if (TYPE_UINT64.Equals(type)) { return(BaseDataConverter.ToUInt64(buffer)); } if (TYPE_DECIMAL.Equals(type)) { return(BaseDataConverter.ToDecimal(buffer)); } if (TYPE_STRING.Equals(type)) { return(Encoding.UTF8.GetString(buffer)); } if (type.IsArray) { return(this.DeserializeArray(type, buffer)); } return(this.DeserializeObject(type, buffer)); }