G
G Giorgio
Guest
I have written two simple classes (clsMessage and clsHello). I create an instance of clsMessage and call the Message1 function passing the specific clsHello class that exposes the correct MessagBox to me. When call then Message2 function passing a generic object that expose the correct MessageBox.
these are the classes:
Public Class clsMessage
Public Function Message1(Of T As {clsHello, IHello})(ByVal value As String) As T
Return (New clsHello(value))
End Function
Public Function Message2(Of T)(ByVal value As String) As T
Return DirectCast((New clsHello(value)), Object)
End Function
End Class
Public Class clsHello
Implements IHello
Dim _value As String
Sub New(ByVal value As String)
_value = value
End Sub
Public Sub Hello() Implements IHello.Hello
Call MsgBox(String.Concat("Hello: ", _value))
End Sub
End Class
Public Interface IHello
Sub Hello()
End Interface
these are code that works:
Dim objMessage As New clsMessage
Dim objHelloSpecific As clsHello = objMessage.Message1(Of clsHello)("My name specific")
objHelloSpecific.Hello()
objHelloSpecific = Nothing
Dim objHelloGeneric As Object = objMessage.Message2(Of Object)("My name generic")
objHelloGeneric.Hello
objHelloGeneric = Nothing
objMessage = Nothing
But if using reflection not works and I don't know how to write the invoke method:
Dim typMessage As System.Type = Reflection.Assembly.GetExecutingAssembly.GetType("WindowsApplication1.clsMessage")
Dim typHello As System.Type = Reflection.Assembly.GetExecutingAssembly.GetType("WindowsApplication1.clsHello")
Dim refMessage As Object = typMessage.GetConstructor({}).Invoke({})
Dim funHelloSpcecific As Reflection.MethodInfo = typMessage.GetMethod("Message1")
Dim funHelloGeneric As Reflection.MethodInfo = typMessage.GetMethod("Message2")
Try
Dim refHelloSpecific As Object = funHelloSpcecific.Invoke(Nothing, {"My name specific"})
refHelloSpecific.Hello()
refHelloSpecific = Nothing
Catch ex As Exception
Call MsgBox(String.Concat("refHelloSpecific: ", ex.Message))
End Try
funHelloSpcecific = Nothing
Try
Dim refHelloGeneric As Object = funHelloGeneric.Invoke(Nothing, {"My name generic"})
refHelloGeneric.Hello
refHelloGeneric = Nothing
Catch ex As Exception
Call MsgBox(String.Concat("refHelloGeneric: ", ex.Message))
End Try
funHelloGeneric = Nothing
refMessage = Nothing
typHello = Nothing
typMessage = Nothing
The error is: Impossibile eseguire operazioni con associazione tardiva in tipi o metodi per i quali ContainsGenericParameters è true (Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true)
question: can someone help me to write the invoke method correctly? thank you.
Giorgio
Continue reading...
these are the classes:
Public Class clsMessage
Public Function Message1(Of T As {clsHello, IHello})(ByVal value As String) As T
Return (New clsHello(value))
End Function
Public Function Message2(Of T)(ByVal value As String) As T
Return DirectCast((New clsHello(value)), Object)
End Function
End Class
Public Class clsHello
Implements IHello
Dim _value As String
Sub New(ByVal value As String)
_value = value
End Sub
Public Sub Hello() Implements IHello.Hello
Call MsgBox(String.Concat("Hello: ", _value))
End Sub
End Class
Public Interface IHello
Sub Hello()
End Interface
these are code that works:
Dim objMessage As New clsMessage
Dim objHelloSpecific As clsHello = objMessage.Message1(Of clsHello)("My name specific")
objHelloSpecific.Hello()
objHelloSpecific = Nothing
Dim objHelloGeneric As Object = objMessage.Message2(Of Object)("My name generic")
objHelloGeneric.Hello
objHelloGeneric = Nothing
objMessage = Nothing
But if using reflection not works and I don't know how to write the invoke method:
Dim typMessage As System.Type = Reflection.Assembly.GetExecutingAssembly.GetType("WindowsApplication1.clsMessage")
Dim typHello As System.Type = Reflection.Assembly.GetExecutingAssembly.GetType("WindowsApplication1.clsHello")
Dim refMessage As Object = typMessage.GetConstructor({}).Invoke({})
Dim funHelloSpcecific As Reflection.MethodInfo = typMessage.GetMethod("Message1")
Dim funHelloGeneric As Reflection.MethodInfo = typMessage.GetMethod("Message2")
Try
Dim refHelloSpecific As Object = funHelloSpcecific.Invoke(Nothing, {"My name specific"})
refHelloSpecific.Hello()
refHelloSpecific = Nothing
Catch ex As Exception
Call MsgBox(String.Concat("refHelloSpecific: ", ex.Message))
End Try
funHelloSpcecific = Nothing
Try
Dim refHelloGeneric As Object = funHelloGeneric.Invoke(Nothing, {"My name generic"})
refHelloGeneric.Hello
refHelloGeneric = Nothing
Catch ex As Exception
Call MsgBox(String.Concat("refHelloGeneric: ", ex.Message))
End Try
funHelloGeneric = Nothing
refMessage = Nothing
typHello = Nothing
typMessage = Nothing
The error is: Impossibile eseguire operazioni con associazione tardiva in tipi o metodi per i quali ContainsGenericParameters è true (Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true)
question: can someone help me to write the invoke method correctly? thank you.
Giorgio
Continue reading...