G
Guest
I'm not sure if I'm having a moment of non-clarity or not but I don't believe
implementing ISerialization works at all.
It started when I had a class that I wanted to provide my own serialization
for because it had ReadOnly properties. So I implemented the ISerializable
interface.
I through a breakpoint into the GetObjectData() method and the New(info,
context) constructor and the breakpoint was not hit.
At this point, I realized that the automatic serialization was being
performed and not the ISerialization interface. Here's sample code:
' This is the class that supposed to be "ISerializable"
Imports System.Runtime.Serialization
Imports System.Xml.Serialization
<Serializable()> _
Public Class SerializableObject
Implements ISerializable
Private _TestData As String
Private _HiddenData As String
Public Property TestData() As String
Get
Return _TestData
End Get
Set(ByVal value As String)
_TestData = value
End Set
End Property
Public Sub New()
End Sub
Protected Sub New(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext)
'Try putting a breakpoint here, it won't be hit.
_TestData = info.GetString("TestData")
_HiddenData = info.GetString("HiddenData")
End Sub
Public Sub GetObjectData(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext) Implements
System.Runtime.Serialization.ISerializable.GetObjectData
'Try putting a breakpoint here, it won't be hit.
info.AddValue("TestData", _TestData, GetType(String))
info.AddValue("HiddenData", "hidden", GetType(String))
End Sub
End Class
Here's some code to test the serialization in the Form_Load event so you
don't have to click anything to test it:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim s As New XmlSerializer(GetType(SerializableObject))
Dim o As New SerializableObject
o.TestData = "Test"
'Serialize first.
Dim sb As New System.Text.StringBuilder
Dim sw As New StringWriter(sb)
s.Serialize(sw, o)
sw.Close()
'Clear the object to make sure there's nothing up my sleeve.
o = Nothing
'Deserialize the object.
Dim sr As New StringReader(sb.ToString)
o = s.Deserialize(sr)
TextBox1.Text = o.TestData
'Take a look at the XML generated. You'll see that the property
called "Hidden" isn't there.
'That means that custom serialization didn't run.
MessageBox.Show(sb.ToString)
End Sub
If ANYONE knows what's going on and how I can get ISerialization to work for
me, please let me know. I've tried all the MSDN and google links to no avail.
Thanks.
implementing ISerialization works at all.
It started when I had a class that I wanted to provide my own serialization
for because it had ReadOnly properties. So I implemented the ISerializable
interface.
I through a breakpoint into the GetObjectData() method and the New(info,
context) constructor and the breakpoint was not hit.
At this point, I realized that the automatic serialization was being
performed and not the ISerialization interface. Here's sample code:
' This is the class that supposed to be "ISerializable"
Imports System.Runtime.Serialization
Imports System.Xml.Serialization
<Serializable()> _
Public Class SerializableObject
Implements ISerializable
Private _TestData As String
Private _HiddenData As String
Public Property TestData() As String
Get
Return _TestData
End Get
Set(ByVal value As String)
_TestData = value
End Set
End Property
Public Sub New()
End Sub
Protected Sub New(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext)
'Try putting a breakpoint here, it won't be hit.
_TestData = info.GetString("TestData")
_HiddenData = info.GetString("HiddenData")
End Sub
Public Sub GetObjectData(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext) Implements
System.Runtime.Serialization.ISerializable.GetObjectData
'Try putting a breakpoint here, it won't be hit.
info.AddValue("TestData", _TestData, GetType(String))
info.AddValue("HiddenData", "hidden", GetType(String))
End Sub
End Class
Here's some code to test the serialization in the Form_Load event so you
don't have to click anything to test it:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim s As New XmlSerializer(GetType(SerializableObject))
Dim o As New SerializableObject
o.TestData = "Test"
'Serialize first.
Dim sb As New System.Text.StringBuilder
Dim sw As New StringWriter(sb)
s.Serialize(sw, o)
sw.Close()
'Clear the object to make sure there's nothing up my sleeve.
o = Nothing
'Deserialize the object.
Dim sr As New StringReader(sb.ToString)
o = s.Deserialize(sr)
TextBox1.Text = o.TestData
'Take a look at the XML generated. You'll see that the property
called "Hidden" isn't there.
'That means that custom serialization didn't run.
MessageBox.Show(sb.ToString)
End Sub
If ANYONE knows what's going on and how I can get ISerialization to work for
me, please let me know. I've tried all the MSDN and google links to no avail.
Thanks.