Logo
programming4us
programming4us
programming4us
programming4us
Windows XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
 
programming4us
Windows 7

Microsoft Visio 2010 : Custom Serialization

6/13/2011 5:36:00 PM
In most cases the .NET built-in serialization engine is good enough. But if it does not meet your particular needs, you can override the serialization process with custom serialization. Basically this means implementing the ISerializable interface that requires the implementation of the GetObjectData method. Such a method is important because it is invoked during serialization. Moreover a custom implementation of the class constructor must be provided. Basically you have to first reproduce at least what built-in formatters do during serialization. Code in Listing 1 shows how to provide custom serialization for the Person class.
Listing 1. Providing Custom Serialization
Imports System.Runtime.Serialization
Imports System.Security.Permissions

<Serializable()>
Public Class Person
Implements ISerializable

Public Overridable Property FirstName As String
Public Overridable Property LastName As String
Public Overridable Property Age As Integer

<SecurityPermission(SecurityAction.Demand,
SerializationFormatter:=True)>
Protected Sub GetObjectData(ByVal info As System.Runtime.Serialization.
SerializationInfo,
ByVal context As System.Runtime.Serialization.
StreamingContext) _
Implements System.Runtime.Serialization.ISerializable.
GetObjectData

info.AddValue("First name", Me.FirstName)
info.AddValue("Last name", Me.LastName)
info.AddValue("Age", Me.Age)
End Sub

'At deserialization time
Protected Sub New(ByVal info As SerializationInfo,
ByVal context As StreamingContext)
MyBase.New()
Me.FirstName = info.GetString("First name")
Me.LastName = info.GetString("Last name")
Me.Age = info.GetInt32("Age")
End Sub
End Class


The GetObjectData method is basically invoked when you pass an object to the Serialize method of a formatter and require an info argument of type SerializationInfo. This class stores all information needed for serialization. It exposes an AddValue method that stores data and a value utilized for recognizing data. Notice that the information is retrieved by the special constructor implementation that is invoked at deserialization time via GetXXX methods where XXX corresponds to .NET types such as Int32, Boolean, Short, and so on. Also notice how GetObjectData is decorated with the SecurityPermission attribute demanding for permissions about the serialization formatter. This is necessary because the permission is allowed only to full-trusted code, thus intranet and Internet zones are not allowed. Both GetObjectData and the constructor are Protected so that derived classes can still take advantage of them but are prevented from being public. If you are sure that your class will not be inherited, GetObjectData can also be Private.

Inheritance Tip

When you create a class that inherits from another class where ISerializable is implemented, if you add new members, you can also provide a new implementation of both GetObjectData and the constructor.


Implementing ISerializable is not the only way for controlling serialization. You can control serialization events, too.

Serialization Events

The serialization process raises four events, which are summarized in Table 1.

Table 1. Serialization Events
EventDescription
OnSerializingOccurs just before serialization begins
OnSerializedOccurs just after serialization completes
OnDeserializingOccurs just before deserialization begins
OnDeserializedOccurs just after deserialization completes

Serialization events are handled differently than classic events. There is an attribute for each event that you can handle as follows:

'Invoke this method before
'serialization begins
<OnSerializing()>
Private Sub FirstMethod()

End Sub

'Invoke this method after
'serialization completes
<OnSerialized()>
Private Sub SecondMethod()

End Sub

'Invoke this method before
'deserialization begins
<OnDeserializing()>
Private Sub ThirdMethod()

End Sub

'Invoke this method after
'deserialization completes
<OnDeserialized()>
Private Sub FourthMethod()

End Sub

The runtime takes care of invoking the specified method according to the moment represented by each attribute. In this way you can provide additional actions based on serialization events.

Other -----------------
- Microsoft Visio 2010 : Connecting Shapes with Dynamic Connectors
- Microsoft Visio 2010 : Copying and Pasting Shapes & Connecting Shapes with Lines
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Creating Office Documents on Windows Live
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Setting Folder Permissions on Windows Live
- Visual Basic 2010 : XML Serialization
- Visual Basic 2010 : Objects Serialization (part 2) - Soap Serialization & Providing Serialization for Custom Objects
- Visual Basic 2010 : Objects Serialization (part 1) - Binary Serialization
- Microsoft Excel 2010 : Editing a Chart & Moving and Resizing a Chart
- Microsoft Excel 2010 - Choosing the Right Type of Chart & Creating a Chart
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Working with Folders on Windows Live
 
 
Video tutorials
- How To Install Windows 8 On VMware Workstation 9

- How To Install Windows 8

- How To Install Windows Server 2012

- How To Disable Windows 8 Metro UI

- How To Change Account Picture In Windows 8

- How To Unlock Administrator Account in Windows 8

- How To Restart, Log Off And Shutdown Windows 8

- How To Login To Skype Using A Microsoft Account

- How To Enable Aero Glass Effect In Windows 8

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
programming4us programming4us
 
programming4us
Women
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone