gpt4 book ai didi

vb.net - 序列化一个泛型类(不需要 XML 帮助类)

转载 作者:行者123 更新时间:2023-12-01 05:23:26 25 4
gpt4 key购买 nike

我有一个关于泛型类的序列化的问题。

因为我想为我的所有配置(它被序列化为 XML)使用一个通用类,所以我编写了以下类:

Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Imports System.Reflection
Imports System.Reflection.Assembly

<Serializable()> _
Public Class GenericConfig(Of T)
Public Sub WriteToFile(ByVal FileName As String)
ConfigVersion = ProgrammVersion.ToString
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Create)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(GenericConfig(Of T)), New Type() {GetType(GenericConfig(Of T))})
serialize.Serialize(XmlFile, Me)
XmlFile.Close()
End Sub

Public Shared Function ReadFromFile(ByVal FileName As String) As GenericConfig(Of T)
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Open)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(GenericConfig(Of T)))
ReadFromFile = serialize.Deserialize(XmlFile)
XmlFile.Close()
Return ReadFromFile
End Function

Public Shared ReadOnly Property ConfigFileName() As String
Get
Return GetExecutingAssembly.Location.Substring(0, GetExecutingAssembly.Location.Length - 4) & ".Config.xml"
End Get
End Property
End Class

Public Class ToolConfig
Inherits GenericConfig(Of ToolConfig)

Public Property Key1 As String = "Value 1"
End Class

我遇到的问题是我无法序列化它,因为在行中出现以下错误
serialize.Serialize(XmlFile, Me)

{“不需要 GenericConfigClass.ToolConfig 类型。使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型。”}

如果您在派生类上使用 XMLSerializer,我已经搜索并找到了很多与该错误相关的文章。我不是在这里做的。我在通用类上使用它。

除了在这里创建一个像这个家伙这样的 XML 帮助器类之外,还有其他方法吗:
http://www.codeproject.com/Articles/35925/Generic-XML-Serialization-Methods

谢谢,
沃尔夫冈

更新:
我是这样称呼它的(简短示例)
Imports System.IO

Public Class frmMain
Dim Config As ToolConfig

Private Sub btnConfigInit_Click(sender As Object, e As EventArgs) Handles btnConfigInit.Click
'----------------- read config file -----------------
If File.Exists(ToolConfig.ConfigFileName) Then
Config = ToolConfig.ReadFromFile(ToolConfig.ConfigFileName)
Else
Config = New ToolConfig
End If
Config.WriteToFile(ToolConfig.ConfigFileName)

MsgBox(Config.Key1)
End Sub
End Class

最佳答案

是的,您正在使用派生类:ToolConfig .通过 XmlInclude 告知你应该没问题:

添加 <XmlInclude(GetType(ToolConfig))>GenericConfig .

另一种解决方案应该是简单地使用 Me.GetType()而不是 GetType(GenericConfig(Of T))创建 XmlSerializer 时实例:

Dim serialize As XmlSerializer = New XmlSerializer(Me.GetType())

为了能够成功反序列化,您必须创建共享 ReadFromFile方法要么是实例方法,要么使其成为通用方法。

如果您想保持该反序列化方法共享,您可以这样做:
Public Class GenericConfig ' <- No (Of T) here! But in the line below.
Public Shared Function ReadFromFile(Of T)(ByVal FileName As String) As T
Dim XmlFile As FileStream = New FileStream(FileName, FileMode.Open)
Dim serialize As XmlSerializer = New XmlSerializer(GetType(T))
ReadFromFile = serialize.Deserialize(XmlFile)
XmlFile.Close()
Return ReadFromFile
End Function
End Class

用法:
GenericConfig.ReadFromFile(Of ToolConfig)(filename)

关于vb.net - 序列化一个泛型类(不需要 XML 帮助类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15201562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com