我正在使用Enterprise library 5.0,實際上是日誌塊。在某些情況下,我處理異常下面如果與遠程端點連接不是全成:無法將System.Collections.Hashtable強制轉換爲類型System.Collections.Generic.IDictionary
Public Function Connect(ByVal sender As Socket) As Integer
Dim result As Integer
result = -1
Try
' Connect the socket to the remote endpoint.
sender.Connect(remoteEP)
Logger.Write("Socket connected to remote endpoint {0}", _
sender.RemoteEndPoint.ToString())
result = 0
Catch ... another exception
Catch ... another exception
Catch ex As System.Net.Sockets.SocketException
Using New Tracer(Common.LOGGING_SOCKETCONNECTIONERROR)
Dim contextInfo As IDictionary = New Hashtable()
contextInfo.Add("Additional Info (Stack Trace)", ex.StackTrace)
Dim provider As DebugInformationProvider = New DebugInformationProvider()
provider.PopulateDictionary(contextInfo)
Dim logEntry As LogEntry = New LogEntry()
logEntry.Categories.Add(Common.LOGGING_CRITICAL_ERRORS_CATEGORY)
logEntry.Message = String.Format("An error occurred when attempting to access the socket: {0}", ex.Message)
logEntry.ExtendedProperties = contextInfo
Logger.Write(logEntry)
End Using
End Try
Return result
End Function
當異常以上燃煤我行得到一個轉換錯誤:
provider.PopulateDictionary(contextInfo)
的轉換誤差爲以下:
System.InvalidCastException was unhandled
Message="Unable to cast object of type 'System.Collections.Hashtable' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'."
我在做什麼錯?
你的函數PopulateDictionary需要IDictionary或IDictionary(TKey,TValue)嗎?它應該期望IDictionary我相信 –
好吧,PopulateDictionary具有以下簽名:Public Sub PopulateDictionary(ByVal dict As System.Collections.Generic.IDictionary(Of String,Object)) – user1624552
和contextInfo是一個Hashtable,實現System.Collection.IDictionary。 ..所以改變你的函數的簽名,一切都應該工作OK ... –