Herfried K. Wagner [MVP] wrote:
"schneider" <ab*@123.com> schrieb:
Have an issue with a property using the DefaultValue(True) attribute.
[...]
here's the steps to reproduce the problem:
1. User UI sets the Class1.test=false during runtime.
2. I serialize Class1 to xml and the false value is saved.
3. User UI sets the Class1.test=true during runtime.
4. I serialize Class1 to xml and the True value is NOT saved and the
False value is still there.
Step 4 is the problem, the file does not seem to reflect the current
state of the class.
\\\
Imports System.ComponentModel
.
.
.
< _
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Visible), _
DefaultValue(True) _
_
Public Property...
///
Does not seem to work, I include a more detailed sample (must be missing
something):
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim c As New Class1
c.test = False
Class1.SerializeObjectToFile("C:\test.txt", c)
c.test = True
Class1.SerializeObjectToFile("C:\test.txt", c)
Dim b As Class1
b = DirectCast(Class1.DeserializeObjectFromFile("C:\te st.txt"), Class1)
Dim a As Integer
a = 1
End Sub
Imports System.ComponentModel
Imports System.IO
Public Class Class1
Private m_testValue As Boolean
<DefaultValue(True),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Visible)> _
Public Property test() As Boolean
Get
Return m_testValue
End Get
Set(ByVal value As Boolean)
m_testValue = value
End Set
End Property
Public Shared Sub SerializeObjectToFile(ByVal fileName As String, ByVal
value As Object)
Dim writer As New StreamWriter(fileName)
Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serialization.XmlSerializer(value.GetType)
' Serialize the object, and close the StreamWriter.
s.Serialize(writer, value)
Catch
Throw
Finally
writer.Close()
End Try
End Sub
Public Shared Function DeserializeObjectFromFile(ByVal fileName As
String) As Object
Dim fs As New IO.FileStream(fileName, FileMode.Open)
Try
Dim w As New Xml.Serialization.XmlSerializer(GetType(Class1))
Dim g As Object = w.Deserialize(fs)
Return g
Catch
Throw
Finally
fs.Close()
End Try
End Function
End Class