无法将类型 'System.Object' 的对象转换为类型 (MyClass) Vb.Net

Unable to cast object of type 'System.Object' to type (MyClass) Vb.Net

我正在使用此代码将我的 class“设备”的对象存储到对象,但我收到 "system.invalidcastexception" 异常。我在其他 classes 中以相同的方式投射它并且在那里有效但在这里它不起作用

Public Class Device
Inherits MainDevice

Private TestData As New SortedList(Of Integer, DataVPAA)
Public Sub New(ByVal version As String, ByVal System As String, ByVal IdNumber As UInteger, ByVal Serial As String)
    DataVersion = version
    SystemVersion = System
    Serial_Number = Serial
    IdentNumber = IdNumber

这是我收到错误的地方

    Dim obj As Object
    obj = LoadXml(GetType(Device), Path)
    If obj Is Nothing Then
        ' Some Logic Here
    Else
        Dim dev As New Device
        dev = CType(obj, Device) '**system.invalidcastexception**

        Me.TestData = dev.TestData
        ' Some Logic Here
    End If

End Sub
End Class

加载函数

Function LoadXML(ByVal DeviceType As Type, ByVal Path As String) As Object
Dim obj As New Object
Dim XMLFilePath As String
Dim xmlreader As XmlReader

If Me.GetType = GetType(ABCDevice) Or Me.GetType = GetType(CVDevice) Or Me.GetType = GetType(CV2Device) Then
    XMLFilePath = Path + "\" + strIdentNr + "_" + Serial_Number + ".xml"
Else
    XMLFilePath = Path + "\" + IdentNumber.ToString + "_" + Serial_Number + ".xml"
End If

'Check if File exists
If File.Exists(XMLFilePath) Then
    Dim fs As New FileStream(XMLFilePath, FileMode.Open, FileAccess.Read)
    xmlreader = XmlReader.Create(fs)
    Try 'Try to deserialize to object
        Dim xml_deserializer As New Serialization.XmlSerializer(DeviceType)
        If xml_deserializer.CanDeserialize(xmlreader) Then

            obj = xml_deserializer.Deserialize(xmlreader)

        End If
    Catch ex As Exception
        MessageBox.Show("XML Deserializer Error: " + ex.Message)
    End Try
    fs.Close()
    Return obj
Else : Return Nothing
End If
End Function

我尝试使用 directcast 等不同的方法来投射它,但我遇到了同样的异常。

您可以使 LoadXml 通用并且始终 return 您想要的类型。如

Function LoadXml(Of T)(path As String) As T
    Dim obj As T = Nothing
    Dim XMLFilePath As String

    If Me.GetType = GetType(ABCDevice) Or Me.GetType = GetType(CVDevice) Or Me.GetType = GetType(CV2Device) Then
        XMLFilePath = path + "\" + strIdentNr + "_" + Serial_Number + ".xml"
    Else
        XMLFilePath = path + "\" + IdentNumber.ToString + "_" + Serial_Number + ".xml"
    End If

    'Check if File exists
    If File.Exists(XMLFilePath) Then
        Using fs As New FileStream(XMLFilePath, FileMode.Open, FileAccess.Read)
            Using reader = XmlReader.Create(fs)
                Try 'Try to deserialize to object
                    Dim xml_deserializer As New Serialization.XmlSerializer(GetType(T))
                    If xml_deserializer.CanDeserialize(reader) Then
                        obj = xml_deserializer.Deserialize(reader)
                    End If
                Catch ex As Exception
                    MessageBox.Show("XML Deserializer Error: " + ex.Message)
                End Try
            End Using
        End Using
    End If
    Return obj
End Function
Dim dev = LoadXml(Of Device)("path")

现在dev保证是一个Device。如果没有,则失败