Update: There is a much better way of handling this!
What you need to do is look at the StreamingContext
passed in to GetObjectData
, the StreamingContext.State
property can tell you what kind of serialization is happening. If you get passed the state Clone
or CrossAppDomain
you may be able to reuse the existing handles you have to the unmanaged blob and just pass the handle pointers in to the SerializationInfo
object.
You will need to do a bunch of copying no matter what you choose, but Convert.ToBase64String
is unnecessary, you can just serialize the managed byte[]
you would have passed in to Convert.ToBase64String
and that should be fine.
I do not know if there is any special case efficency's for byte[]
, however all Seralizations get turned in to a byte stream during the transfer process. So by encoding in Base64 you are going to be doing
- Read your unmanaged blob and convert it to a managed
byte[]
- Convert your
byte[]
in to a string
via ToBase64String
(increasing the size by a factor of 4/3 due to the encoding)
- Pass the encoded string with a tag name in to the
SerializationInfo
- Pass the rest of the metadata in to the
SerializationInfo
with their respetive tags
- The serializer takes all of the tag+object combos and turns them in to a byte stream to be passed on to the destination
- The deserializer takes the byte stream and turns it back in to a collection of tags and objects
- You get your
string
back using the same tag name
- You get your metadata back using the tags you saved them under.
- Convert the
string
back in to a byte[]
via FromBase64String
- Convert the
byte[]
back in to the unmanaged binary blob.
If you never encode to a string and just pass the byte[]
in as the object to the SerializationInfo
object the steps are pretty much the same, it just does not have that 4/3 size increase of the datablob to encode the binary data as text.
- Read your unmanaged blob and convert it to a managed
byte[]
- Pass the
byte[]
with a tag name in to the SerializationInfo
- Pass the rest of the metadata in to the
SerializationInfo
with their respetive tags
- The serializer takes all of the tag+object combos and turns them in to a byte stream to be passed on to the destination
- The deserializer takes the byte stream and turns it back in to a collection of tags and objects
- You get your
byte[]
back using the same tag name
- You get your metadata back using the tags you saved them under.
- Convert the
byte[]
back in to the unmanaged binary blob.
So I guess the "optimization" is transmitting as a byte[]
has the benefit of not increasing the amount of data you have to transmit to the desearizer.