0

I have an application in VB.NET When I run the application in Visual Studio 2010 and mouseover an IAsyncResult, I see the protected property Result. I would like to read the value of the property in the application. How can I do that?

Imports System.Net
Imports System.Net.Sockets
...

Friend Function StartSendGo() As String

    'Declarations
    Dim strSendMachineName As String = "DEV001"
    Dim intSendPort As Integer = 50035
    Dim socketclient As New System.Net.Sockets.TcpClient()


    Dim rslt As IAsyncResult = tcpClient.BeginConnect(strSendMachineName, intSendPort, New AsyncCallback(AddressOf ConnectCallback), socketclient)
    Dim blnSuccess = rslt.AsyncWaitHandle.WaitOne(intTimeOutConnect, True)
    'HERE is where I need rslt.Result.Message

End Function

Public Function ConnectCallback()
    'Placeholder
End Function

When I mouseover rslt, VS shows that it is of type System.Net.Sockets.Socket+MultipleAddressConnectAsyncResult I have never seen a plus (+) in a type before, and I am not able to declare a variable of that type. If I expand the properties, there is a protected property Result, which has a property Message with a value of "No connection could be made because the target machine actively refused it 192.0.0.10:50035". I need access to that message. I would also like to access addresses, but that is less important.

Jacob Quisenberry
  • 1,131
  • 3
  • 20
  • 48
  • The name of the PC and the IP address have been fictionalized. – Jacob Quisenberry Sep 30 '13 at 20:18
  • You're defeating the purpose of async by synchronously waiting for the operation to finish. Don't do that; instead, do the rest of your work in the callback. – SLaks Sep 30 '13 at 20:19
  • Consider upgrading to VS2012 (or the upcoming 2013), which can make this task infinitely easier with the new `Async` keyword and TPL. – SLaks Sep 30 '13 at 20:19
  • 1
    BTW, `+` means a nested type. – SLaks Sep 30 '13 at 20:20
  • Thanks. Since this is a nested type, what is the syntax to declare or cast a nested type? I tried Dim myType As Type = Type.GetType("System.Net.Sockets.Socket+MultipleAddressConnectAsyncResult"), but the result in Nothing. – Jacob Quisenberry Sep 30 '13 at 21:29
  • Unfortunately, I will not be permitted to fix the Async code because this is not my project. – Jacob Quisenberry Sep 30 '13 at 21:31
  • That's a `Private` class; you can't use it outside the outer class. – SLaks Sep 30 '13 at 22:10

1 Answers1

0

I found a solution - to use Reflection to read the value of the private property.

'Imports
Imports System.Reflection


'Call functions that write to rslt
rslt = tcpClient.BeginConnect(strSendMachineName, intSendPort, New AsyncCallback(AddressOf ConnectCallback), socketclient)
blnSuccess = rslt.AsyncWaitHandle.WaitOne(intTimeOutConnect, True)

'Use Reflection
'Get Type
Dim myType As Type = rslt.GetType()
'Get properties
Dim myPropertyInfo As PropertyInfo() = myType.GetProperties((BindingFlags.NonPublic Or BindingFlags.Instance))
'The order of the properties is not guaranteed. Find by name.
For Each pi As PropertyInfo In myPropertyInfo
    If pi.Name = "Result" Then
        'TODO Add check for nothing.
        'Assign to Exception-type variable.
        exException = pi.GetValue(rslt, Nothing)
    End If
Next
Jacob Quisenberry
  • 1,131
  • 3
  • 20
  • 48