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

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

因为我想为我的所有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)

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...