11. Caveats with NonSerialized Attribute
• Any field that is prevented from being serialized by marking it with NonSerialized attribute will be set to its default value after deserialization (0 in the case of integer).
• NonSerialized Attribute works only with Formatters.
• To perform Selective serialization using XmlSerializer, [XmlIgnore] tag needs to be used.
12. Deserialization Callback
• Used for the initialization of fields that were prevented from being serialized.
• To implement Deserialization Callback, the class must implement IDeserializationCallback interface.
• IDeserializationCallback contains a method called OnDeserialization(Object Sender) which needs to be implemented in the class which uses the interface.
• OnDeserialization(Object Sender) is the last method which is executed in the Deserialization process and it used to notify the end of the deserialization process.
13. Custom Serialization
• Custom serialization using ISerializable interface
• Caveats with ISerializable
The ISerializable interface consists of a single method, GetObjectData, which accepts two parameters.
14. Custom Serialization using ISerializable Interface
Objects can control serialization by implementing ISerializable interface.
interface ISerializable
{
void GetObjectData(SerializationInfo info,
StreamingContext context);
}
SerializationInfo - contains three properties namely AssemblyName, FullTypeName and MemberCount.
Streaming Contexts - Describes the source and destination of the serialized stream
15. Serialization using GetObjectData()
• In the implementation of GetObjectData()
_ Add each value that must be serialized to the SerializationInfo object (sent as parameter).
_ If a field belongs to a type/class that implements ISerializable, only then add the value
_ Formatter will realize this and call GetObjectData() on the added object.
• Always call the overloaded AddValue() of SerializationInfo to add the values to be serialized.
• Code Snippet:
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("i", n1);
info.AddValue("j", n2);
info.AddValue("k", str);
}
16. De-serialization with ISerializable
Requires a special constructor of the form
classname(SerializationInfo info, StreamingContext context) { … }
• Formatter calls the constructor passing serialization information and the context when deserializing.
Code Snippet
protected MyObject(SerializationInfo info, StreamingContext context)
{
n1 = info.GetInt32("i");
n2 = info.GetInt32("j");
str = info.GetString("k");
}
Note: In addition to implementing the ISerializable interface, the class must still be declared as Serializable, in order for the formatter to accept the class even before checking for the ISerializable implementation. The only advantage of implementing ISerializable is that it provides more finer degree of control.
17. Binary Vs SOAP Vs XML – A Comparison
Note • For Remoting and Web Services, the process of serialization and deserialization is automatically taken care of by the framework. No explicit transformations are generally required.
• The namespaces System.Runtime.Remoting and System.Web.Services.WebServices contain various classes and methods which automate the process of serialization and deserialization.
阅读全文
类别:.net summary 查看评论
 
                    
                 


 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号