具有所有属性的控件的(反)序列化

问题描述 投票:0回答:1

我正在尝试序列化控件的所有属性,对于初学者,我从 Devexpress 的 TextEdit 控件开始。但是,TextBox 控件也会出现同样的问题。序列化仅传递给没有从属项或不是枚举器的属性,即它们可以通过(反序列化)返回字符串。问题是,有没有人处理过这个问题,有没有比为所有控制属性编写单独的规则更简单的方法。我开始写了,但是太难了,即使考虑到不同的控件(来自不同的制造商),这确实是一项太难的工作。


   Public Class ControlProperties
       Public Property Text As String
       Public Property BackColor As String
       Public Property BorderStyle As Integer
       Public Property Size As Size
       Public Property Location As Point
       Public Property ForeColor As String
       Public Property Multiline As Boolean
       Public Property AllowDrop As Boolean
       Public Property Cursor As String
       Public Property RightToLeft As RightToLeft
       Public Property Font As SerializableFont
   End Class

   Public Class SerializableFont
       Public Property Name As String
       Public Property Size As Single
       Public Property Style As FontStyle

       Public Sub New()
       End Sub

       Public Sub New(font As Font)
           Name = font.Name
           Size = font.Size
           Style = font.Style
       End Sub

       Public Function ToFont() As Font
           Return New Font(Name, Size, Style)
       End Function
   End Class

   Public Sub SerializeControlProperties(control As Object, filePath As String)
       Dim properties As New ControlProperties()
       properties.Text = control.Text
       properties.BackColor = ColorTranslator.ToHtml(control.BackColor)
       properties.BorderStyle = control.BorderStyle
       properties.Size = control.Size
       properties.Location = control.Location
       properties.ForeColor = ColorTranslator.ToHtml(control.ForeColor)
       properties.AllowDrop = control.AllowDrop
       properties.Cursor = CursorToString(control.Cursor)
       properties.RightToLeft = control.RightToLeft
       properties.Font = New SerializableFont(control.Font)

       Dim serializer As New XmlSerializer(GetType(ControlProperties))

       Using writer As TextWriter = New StreamWriter(filePath)
           serializer.Serialize(writer, properties)
       End Using
   End Sub
<?xml version="1.0" encoding="utf-8"?>
<ControlProperties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Text />
  <BackColor>#FFFFFF</BackColor>
  <BorderStyle>7</BorderStyle>
  <Size>
    <Width>100</Width>
    <Height>20</Height>
  </Size>
  <Location>
    <X>221</X>
    <Y>26</Y>
  </Location>
  <ForeColor>#282828</ForeColor>
  <Multiline>false</Multiline>
  <AllowDrop>false</AllowDrop>
  <Cursor>System.Windows.Forms.Cursor</Cursor>
  <RightToLeft>No</RightToLeft>
  <Font>
    <Name>Tahoma</Name>
    <Size>8.25</Size>
    <Style>Regular</Style>
  </Font>
</ControlProperties>
vb.net winforms xml-deserialization
1个回答
0
投票

XML 有点过时,我们现在通常会序列化为 JSON,使用

System.Text.Json
nuget 包非常简单:

'Add nuGet package System.Text.Json
Dim ctl As New ControlProperties With {.Text = "Test", .BackColor = "Black"}
'serialize
Dim serializedTextBox = JsonSerializer.Serialize(ctl)
System.IO.File.WriteAllText("C:\Junk\Junk.json", serializedTextBox)
'deserialize
Dim ctl2 = JsonSerializer.Deserialize(Of ControlProperties)(serializedTextBox)
© www.soinside.com 2019 - 2024. All rights reserved.