Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ISerializationSurrogate.SetObject return an object as well as take an object to populate?

I was recently looking at Runtime Serialization and came across the ISerializationSurrogate interface. I am confused about it's SetObject method however.

The signature is:

object SetObjectData(object obj, System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.ISurrogateSelector selector)

and the help information is:

Summary:

Populates the object using the information in the System.Runtime.Serialization.SerializationInfo.

Parameters:

obj: The object to populate.

info: The information to populate the object.

context: The source from which the object is deserialized.

selector: The surrogate selector where the search for a compatible surrogate begins.

Returns:

The populated deserialized object.

If obj is the object to populate, why does it return The populated deserialized object? In one example from MSDN they populate obj and return null.

like image 459
Robert Davey Avatar asked Oct 11 '11 09:10

Robert Davey


1 Answers

Check this .NET column article: http://msdn.microsoft.com/en-us/magazine/cc188950.aspx

It says:

Notice that the SetObjectData method has a return type of Object. This would make you think that SetObjectData could actually return a reference to a completely different type of object. However, the Microsoft SoapFormatter and BinaryFormatter types ignore this return value completely, which is why I returned null in the previous example. Obviously, there is a bug here; the return value should allow SetObjectData to return a different object.

Microsoft has scheduled this bug for repair. If the return value is null, then the formatter will use the object that it passed to SetObjectData. If SetObjectData returns an object reference, then the formatter will use the object that's returned. Unfortunately, until this bug is fixed, you can't deserialize a value type.

like image 116
Kaifei Avatar answered Oct 24 '22 04:10

Kaifei