I having some issues with converting a large byte[] array into a strongly typed array.
- I have an array which has been concatinated into one large byte[] array and stored in a table.
- I want to then read this byte[] array but convert it to a strongly typed array.
As I have stored the entire array as a byte[] array, can I not read that byte array and convert it to my strongly typed version? At the moment its returning null…
Is this possible in one hit?
Thanks in advance, Onam.
<code>
#region Save
public void Save<T>(T[] Array) where T : new()
{
List<byte[]> _ByteCollection = new List<byte[]>();
byte[] _Bytes = null;
int _Length = 0;
int _Offset = 0;
foreach (T _Item in Array)
{
_ByteCollection.Add(Serialise(_Item));
}
foreach (byte[] _Byte in _ByteCollection)
{
_Length += _Byte.Length;
}
_Bytes = new byte[_Length];
foreach (byte[] b in _ByteCollection)
{
System.Buffer.BlockCopy(b, 0, _Bytes, _Offset, b.Length);
_Offset += b.Length;
}
Customer[] c = BinaryDeserialize<Customer[]>(_Bytes);
}
#endregion
#region BinaryDeserialize
public static T BinaryDeserialize<T>(byte[] RawData)
{
T _DeserializedContent = default(T);
BinaryFormatter _Formatter = new BinaryFormatter();
try
{
using (MemoryStream _Stream = new MemoryStream())
{
_Stream.Write(RawData, 0, RawData.Length);
_Stream.Seek(0, SeekOrigin.Begin);
_DeserializedContent = (T)_Formatter.Deserialize(_Stream);
}
}
catch (Exception ex)
{
_DeserializedContent = default(T);
}
return _DeserializedContent;
}
#endregion
</code>
I think the problem is that you are serializing each item to a list, then concatenating the bytes. When this is deserialised this just looks like the data for one customer plus some unexpected data (the other customers) at the end.
I don’t know how your serialize method works but you can probably just change code:
To:
And that should work, then you could probably simplify it a little.