【发布时间】: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
【问题讨论】:
-
代码太多。请缩短它。