c# 4.0 - object reference after Serialization and De-Serialization -
why after de-serialization object reference not same before serilization.
ok let me explain this
i having string
string test = "teststring";
now serilization
datacontractjsonserializer _datacontractjsonserializer = new datacontractjsonserializer(typeof(string)); memorystream ms = new memorystream(); _datacontractjsonserializer.writeobject(ms, test); var jsonstring = encoding.default.getstring(ms.toarray());
now deserilization jsonstring actual value
memorystream ms1 = new memorystream(encoding.unicode.getbytes(jsonstring)); datacontractjsonserializer serializer = new datacontractjsonserializer(typeof(string)); string deseriliaedstrring = serializer.readobject(ms1) string;
here in deseriliaedstrring got actual value if check there reference not equal
bool isreferenceequal = object.referenceequals(deseriliaedstrring, test);
when serialize , deserialize object, deserialization create instance base on serialized data. of course not same instance original one, seems logical. after all, have following process:
- you have object instance
- you create string instance representation of object instance
- you create object instance based on serialized data in string
if take consideration original instance might have mutated, deserialization can happen in process, in machine, becomes quite clear deserialization not possibly return original instance.
in particular case, when object string
, imagine string interning make system reuse old instance (if deserialization happened within same appdomain). probably* require datacontractjsonserializer
aware of this, make unnecessarily complex.
*) not quite sure of what part of system takes care of string interning.
Comments
Post a Comment