【问题标题】:Unable to cast object of type 'System.Collections.Generic.List`1[' to type 'System.Collections.Generic.IEnumerable`1['无法将“System.Collections.Generic.List`1[”类型的对象转换为“System.Collections.Generic.IEnumerable`1[”类型
【发布时间】:2015-09-14 00:19:58
【问题描述】:

我在运行时遇到的错误是

Unable to cast object of type
'System.Collections.Generic.List`1[TCPClientClean.ChatClient+LogTime]'
to type
'System.Collections.Generic.IEnumerable`1[TCPClientClean.TimeLogLabel+LogTime]'.

我有一个用于记录事件和保存时间戳的类。然后我想保存在TimeLogLabel 类中创建的列表,然后在启动时重新加载。保存到文本文件已解决并且有效,但我不明白如何将保存的列表恢复到 TimeLogLabel 类中。

Option Strict On
Imports System.Text
Imports System.IO
Imports System.Collections.Generic
Public Class TimeLogLabel
Inherits Label

Private m_VaraibleToSet As Boolean
<System.ComponentModel.Category("Control")> _
Public Property VaraibleToSet() As Boolean
    Get
        Return m_VaraibleToSet
    End Get
    Set(ByVal value As Boolean)
        m_VaraibleToSet = value
    End Set
End Property
Private m_PrefixText As String = "Prefix Text"
<System.ComponentModel.Category("Control")> _
Public Property PrefixText() As String
    Get
        Return m_PrefixText
    End Get
    Set(ByVal value As String)
        m_PrefixText = value
    End Set
End Property
Public Class LogTime
    Private m_EventName As String
    Public Property EventName() As String
        Get
            Return m_EventName
        End Get
        Set(ByVal value As String)
            m_EventName = value
        End Set
    End Property
    Private m_StartT As String
    Public Property StartT() As String
        Get
            Return m_StartT
        End Get
        Set(ByVal value As String)
            m_StartT = value
        End Set
    End Property
    Private m_StopT As String
    Public Property StopT() As String
        Get
            Return m_StopT
        End Get
        Set(ByVal value As String)
            m_StopT = value
        End Set
    End Property
    Private m_TSpan As String
    Public Property TSpan() As String
        Get
            Return m_TSpan
        End Get
        Set(ByVal value As String)
            m_TSpan = value
        End Set
    End Property
    Public Sub New( _
  ByVal m_EventName As String, _
  ByVal m_StartT As String, _
  ByVal m_StopT As String, _
  ByVal m_TSpan As String)
        EventName = m_EventName
        StartT = m_StartT
        StopT = m_StopT
        TSpan = m_TSpan
    End Sub
End Class


Public TimeList As List(Of LogTime) = New List(Of LogTime)

    Public Sub StartTimer()
    If Ons = False Then
        Ons = True
        EventIdInt = EventIdInt + 1
        EventIdStg = ""
        If EventIdInt <= 9 Then
            EventIdStg = "0" & CStr(EventIdInt)
        ElseIf EventIdInt >= 9 Then
            EventIdStg = CStr(EventIdInt)
        End If
        StartTime = Now
        TimeList.Add(New LogTime(PrefixText & " " & EventIdStg,     "StartTime " & StartTime, "", ""))
        Timer.Enabled = True
        Timer.Start()
    End If
End Sub

Public Sub StopTimer()
    If Ons = True Then
        Ons = False
        EventIdInt = EventIdInt + 1
        EventIdStg = ""
        If EventIdInt <= 9 Then
            EventIdStg = "0" & CStr(EventIdInt)
        ElseIf EventIdInt >= 9 Then
            EventIdStg = CStr(EventIdInt)
        End If
        StopTime = Now
        TimeList.Add(New LogTime(PrefixText & " " & EventIdStg, "", _           "Stop Time " & StopTime, " Up Time " & AccTimeStg))
        Timer.Enabled = False
        Timer.Stop()
    End If
End Sub






Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Collections.Generic
Imports System.Linq  
Public Class ChatClient   ' Form that has "TimeLogLabel" on the form


Public Class LogTime
    Private m_EventName As String
    Public Property EventName() As String
        Get
            Return m_EventName
        End Get
        Set(ByVal value As String)
            m_EventName = value
        End Set
    End Property
    Private m_StartT As String
    Public Property StartT() As String
        Get
            Return m_StartT
        End Get
        Set(ByVal value As String)
            m_StartT = value
        End Set
    End Property
    Private m_StopT As String
    Public Property StopT() As String
        Get
            Return m_StopT
        End Get
        Set(ByVal value As String)
            m_StopT = value
        End Set
    End Property
    Private m_TSpan As String
    Public Property TSpan() As String
        Get
            Return m_TSpan
        End Get
        Set(ByVal value As String)
            m_TSpan = value
        End Set
    End Property
    Public Sub New( _
  ByVal m_EventName As String, _
  ByVal m_StartT As String, _
  ByVal m_StopT As String, _
  ByVal m_TSpan As String)
        EventName = m_EventName
        StartT = m_StartT
        StopT = m_StopT
        TSpan = m_TSpan
    End Sub
End Class

Private Sub ChatClient_Load(ByVal sender As System.Object, ByVal e s    System.EventArgs) Handles MyBase.Load

Dim TimeListSaved As List(Of LogTime) = New List(Of LogTime)
    ' Dim TimeListSaved As IEnumerable(Of LogTime) = New List(Of LogTime)

    Dim Sartuptime As DateTime = Now
    TimeListSaved.Add(New LogTime("Prefix&EvebtId 01", "Start Time " & UpStartTime, "Stop Time", "Up Time"))

LAtSvrComm.TimeList.AddRange(TimeListSaved)
' this is where I get my error
    ' Unable to cast object of type 
    'System.Collections.Generic.List`1[TCPClientClean.ChatClient+LogTime]'
    'to type 
    'System.Collections.Generic.IEnumerable`1[TCPClientClean.TimeLogLabel+LogTime]'.
    End Sub

Private Sub ChatClient_FormClosing(ByVal sender As System.Object, ByVal e As 

System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing


Dim FILE_NAME As String = "C:\LogSave.txt"
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

    For Each TimeLogLabel In LAtSvrComm.TimeList
        objWriter.WriteLine(TimeLogLabel.EventName & " * " &     TimeLogLabel.StartT & " * " & TimeLogLabel.StopT & _
                            " * " & TimeLogLabel.TSpan)
    Next TimeLogLabel
    objWriter.WriteLine("end of " & LAtSvrComm.PrefixText)
    End Sub



    objWriter.Close()
end sub

【问题讨论】:

  • 代码太多。请缩短它。

标签: vb.net list class


【解决方案1】:

您声明了 2 个名为 LogTime 的类,而不是 1 个。它们是成员类,彼此之间不会自动转换,因此 CLR 不知道如何为您转换(抛出此错误)。

也就是说,TCPClientClean.ChatClient+LogTimeTCPClientClean.TimeLogLabel+LogTime不是同一类型。

由于您似乎打算只有一个 LogTime 类,请将其声明为一个单独的类(而不是作为另一个类的成员的类)并删除您拥有的 2 个不同的 LogTimes现在。

【讨论】:

  • 我想我明白你的意思,但有一个问题。 TimeLogLabel 是项目中的一个类,而不是另一个类的成员。所以我认为您所说的是向项目添加一个类,然后加载我的列表,然后将该列表添加到 TimeLogLabel.addrange。我没有这样做或没有想到的原因是我不明白为什么我不能添加到 TimeLogLabel.LogTime 类?那是因为无法从表单中添加吗?我会给新的单独的课程。
  • OK 我添加了一个与其他列表类相同的新类 LoadTimeLog。然后我加载事件名称、开始时间、.ect,然后加载“LAtSvrComm.TimeList.AddRange(SvrTimeListSaved)”我现在收到此错误无法转换类型为“System.Collections.Generic.List1[TCPClientClean.LoadTimeLog]' to type 'System.Collections.Generic.IEnumerable1[ TCPClientClean.TimeLogLabel+LogTime]'。
【解决方案2】:

这解决了它。 在表单加载时,我将读取文本文件,然后将字符串放入正确的项目中。

      LAtSvrComm.TimeList.Add(New TimeLogLabel.LogTime("Prefix&EvebtId 01", "Start Time " & UpStartTime, "Stop Time", "Up Time"))

@Billy N 感谢您为我指明了正确的方向。通过解释你所做的方式让我想出了这个解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2011-05-14
    • 2013-05-15
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多