Mio,
A few things.
The first is that you don't really have to initialize the memory stream
with your array when serializing the instance, since it will allocate space
as needed when the serializer writes to it.
The other thing about this is that when you pass the byte array to the
constructor, it makes the memory stream fixed-size. So if you need more
than one kilobyte for the serialized instance, it will throw an exception.
Additionally, when you are returning the byte array, it contains extra
bytes that are not part of the serialized instance (in the case where you
need less than 1K).
Your get code should look like this:
// Create the formatter.
BinaryFormatter formatter = new BinaryFormatter();
// Create the stream.
using (MemoryStream memoryStream = new MemoryStream())
{
// No need to set the position, since it is already at 0.
// Serialize the instance.
formatter.Serialize(memoryStream, Directions);
// Return the array. This will be properly sized.
return memoryStream.ToArray();
}
Now, when dealing with deserializing the instance, your code should work
just fine. However, it could stand to be cleaned up, like so:
// Pass the value into the memory stream, since we are just reading.
using (MemoryStream memoryStream = new MemoryStream(value))
{
// Create the formatter.
BinaryFormatter bf = new BinaryFormatter();
// Set the hashtable.
Directions = (Hashtable) bf.Deserialize(memoryStream);
}
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
<Mi*********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I know how to take a single hashtable, and then use a binaryformatter
and a filestream to dump it to a file, but I need to serialize and
deserialize a hashtable inside a class. I've been trying this:
[XmlIgnore]
public Hashtable Directions;
public byte[] DirectionsSerialized
{
get
{
byte[] bData = new byte[1024];
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream(bData);
memoryStream.Position = 0;
formatter.Serialize(memoryStream, Directions);
return bData;
}
set
{
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(value, 0, value.Length);
BinaryFormatter bf = new BinaryFormatter();
memoryStream.Position = 0;
Directions = (Hashtable)bf.Deserialize(memoryStream);
}
}
Which appears to serialize fine, but upon deserialization, it throws an
error:
"'', hexadecimal value 0x1B, is an invalid character. Line 299,
position 18."
Thanks guys.