当前位置 : 主页 > 编程语言 > c语言 >

vb.net – 序列化泛型类(不需要XML帮助器类)

来源:互联网 收集:自由互联 发布时间:2021-06-24
我有一个关于泛型类序列化的问题. 因为我想为我的所有Configuration(它被序列化为XML)使用泛型类,所以我编写了以下类: Imports System.XmlImports System.Xml.SerializationImports System.IOImports System.R
我有一个关于泛型类序列化的问题.

因为我想为我的所有Configuration(它被序列化为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,我已经google搜索并找到了很多与该错误相关的文章.我不是在这里做的.我在泛型类上使用它.

除了在这里创建像这个伙伴这样的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.

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

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)
网友评论