0

我正在使用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]'." 

我在做什么错?

+0

你的函数PopulateDictionary需要IDictionary或IDictionary(TKey,TValue)吗?它应该期望IDictionary我相信 –

+0

好吧,PopulateDictionary具有以下签名:Public Sub PopulateDictionary(ByVal dict As System.Collections.Generic.IDictionary(Of String,Object)) – user1624552

+1

和contextInfo是一个Hashtable,实现System.Collection.IDictionary。 ..所以改变你的函数的签名,一切都应该工作OK ... –

回答

1

更改:

Dim contextInfo As IDictionary = New Hashtable() 

由:

Dim contextInfo As IDictionary = New Dictionary(Of String, Object) 

解决了这个问题。

+1

是的,这就是我的意思,如果你不能改变函数的签名,改变你传递的参数... –

+0

感谢您指导我在正确的方向;) – user1624552

相关问题