I have a piece of code in front of me implementing a Clone() operation, by using BinaryFormatter:
public object Clone()
{
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
stream.Position = 0;
object result = formatter.Deserialize(stream);
return result;
}
(and NO, I did not write and do not endorse this). Someone using this method has encountered a situation where a call to this method results in a TargetInvocationException
from the Deserialize(stream)
call. The InnerException
is a NullReferenceException
.
What makes this particularly fun -- fun enough to ask for help -- is that I don't have access to what the object being cloned is.
It strikes me that, since everything is in memory, and the Serialize(...)
call succeeds, the data in memory should be valid and deserialize properly. There is no opportunity for a mismatched or missing assembly, or the data being corrupt.
The serialized object may contain events and/or delegates, but my impression is that if these cause any issues, the Serialize(...)
call will fail.
Does anyon开发者_StackOverflowe have any insight into what might cause this? I'm at a complete loss on this one.
精彩评论