Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Overriding the Assembly and/or Type When Deserializing an Object

When serializing an object, formatters output the type’s full name and the full name of the type’s defining assembly. When deserializing an object, formatters use this information to know exactly what type of object to construct and initialize. The earlier discussion about the ISerialization­ Surrogate interface showed a mechanism allowing you to take over the serialization and deserial- ization duties for a specific type. A type that implements the ISerializationSurrogate interface is tied to a specific type in a specific assembly.

However, there are times when the ISerializationSurrogate mechanism doesn’t provide enough flexibility. Here are some scenarios when it might be useful to deserialize an object into a dif- ferent type than it was serialized as:

■ A developer might decide to move a type’s implementation from one assembly to a different assembly. For example, the assembly’s version number changes making the new assembly dif- ferent from the original assembly.

■ An object on a server that gets serialized into a stream that is sent to a client. When the client processes the stream, it could deserialize the object to a completely different type whose code knows how to remotely invoke method calls to the server’s object.

■ A developer makes a new version of a type. We want to deserialize any already-serialized objects into the new version of the type.

The System.Runtime.Serialization.SerializationBinder class makes deserializing an object to a different type very easy. To do this, you first define your own type that derives from the abstract SerializationBinder type. In the following code, assume that version 1.0.0.0 of your assembly defined a class called Ver1 and assume that the new version of your assembly defines the Ver1ToVer2SerializationBinder class and also defines a class called Ver2.

 

internal sealed class Ver1ToVer2SerializationBinder : SerializationBinder { public override Type BindToType(String assemblyName, String typeName) {

// Deserialize any Ver1 object from version 1.0.0.0 into a Ver2 object

 

// Calculate the assembly name that defined the Ver1 type AssemblyName assemVer1 = Assembly.GetExecutingAssembly().GetName(); assemVer1.Version = new Version(1, 0, 0, 0);

 

// If deserializing the Ver1 object from v1.0.0.0, turn it into a Ver2 object if (assemblyName == assemVer1.ToString() && typeName == "Ver1")

return typeof(Ver2);

 

// Else, just return the same type being requested

return Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));

}

}


Now, after you construct a formatter, construct an instance of Ver1ToVer2SerializationBinder and set the formatter’s Binder read/write property to refer to the binder object. After setting the Binder property, you can now call the formatter’s Deserialize method. During deserialization,

the formatter sees that a binder has been set. As each object is about to be deserialized, the format- ter calls the binder’s BindToType method, passing it the assembly name and type that the formatter wants to deserialize. At this point, BindToType decides what type should actually be constructed and returns this type.



       
   
 
 


 


C HA P T E R 2 5


Date: 2016-03-03; view: 865


<== previous page | next page ==>
Nbsp;   Serialization Surrogates | Interoperating with WinRT Components
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)