S
schoenfeld1
I've implemented IPC between two applications using named pipes and
binary serialization, but have noticed that the binary formatter is
rather slow.
It seems that the binary formatter reflects the entire type everytime
it is invoked to serialize/deserialize an object of that type.
Is there a way to prepare the binary formatter with a pre-defined type,
such that it only reflects once but can be re-used to
serialize/deserialize objects of that type without performing
reflection again?
Here is the relevant code:
static public byte[] Serialize(Message message) {
#region Pre-conditions
Debug.Assert(message != null);
#endregion
MemoryStream memStream = new MemoryStream(1024);
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(memStream, message);
return memStream.GetBuffer();
}
static public Message Deserialize(byte[] data) {
#region Pre-conditions
Debug.Assert(data != null);
#endregion
BinaryFormatter binFormatter = new BinaryFormatter();
MemoryStream memStream = new MemoryStream(data);
object obj = binFormatter.Deserialize(memStream);
Debug.Assert(obj is Message);
return obj as Message;
}
Any assistance is appreciated.
binary serialization, but have noticed that the binary formatter is
rather slow.
It seems that the binary formatter reflects the entire type everytime
it is invoked to serialize/deserialize an object of that type.
Is there a way to prepare the binary formatter with a pre-defined type,
such that it only reflects once but can be re-used to
serialize/deserialize objects of that type without performing
reflection again?
Here is the relevant code:
static public byte[] Serialize(Message message) {
#region Pre-conditions
Debug.Assert(message != null);
#endregion
MemoryStream memStream = new MemoryStream(1024);
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(memStream, message);
return memStream.GetBuffer();
}
static public Message Deserialize(byte[] data) {
#region Pre-conditions
Debug.Assert(data != null);
#endregion
BinaryFormatter binFormatter = new BinaryFormatter();
MemoryStream memStream = new MemoryStream(data);
object obj = binFormatter.Deserialize(memStream);
Debug.Assert(obj is Message);
return obj as Message;
}
Any assistance is appreciated.