有没有办法让这种泛型委托的使用更简单?

时间:2011-10-12 15:43:15

标签: .net vb.net delegates

我有以下方法可以使用:

Private Delegate Function WebMethodDelegate(Of TRequest, TResponse)(ByVal request As TRequest) As TResponse

Private Function CallWebMethod(Of TRequest, TResponse)(ByVal request As TRequest, ByVal theMethodToRun As WebMethodDelegate(Of TRequest, TResponse)) As TResponse

    Dim response As TResponse = Nothing

'begin pseudocode
    While somtthing is true

            response = theMethodToRun.Invoke(Request)

    End While
'end pseudocode

End Function

我用(一个丑陋的电话)打电话给上面的人:

Dim webMethodDeletgate As New WebMethodDelegate(Of wsLookupServiceProxy.RequestBase, wsLookupServiceProxy.GetSelectedLookupInfoResponseOfTitle)(AddressOf _service.GetAllTitles)
CallWebMethod(Of wsLookupServiceProxy.RequestBase, wsLookupServiceProxy.GetSelectedLookupInfoResponseOfTitle)(request, webMethodDeletgate)

我想过这样做:

Dim requestType As Type = GetType(wsLookupServiceProxy.RequestBase)
Dim responseType As Type = GetType(wsLookupServiceProxy.GetSelectedLookupInfoResponseOfTitle)

Dim webMethodDeletgate As New WebMethodDelegate(Of requestType, responseType)(AddressOf _service.GetAllTitles)
CallWebMethod(Of requestType, responseType)(request, webMethodDeletgate)

但是编译器不喜欢它。

我想知道是否有人可以提供一种更简洁的方法来调用该方法,而无需进行非常长的方法调用?

提前致谢。

1 个答案:

答案 0 :(得分:1)

好吧,使用

Imports wsLookupServiceProxy

在班级的顶部你可以将其归结为

Dim webMethodDeletgate As New WebMethodDelegate(Of RequestBase, GetSelectedLookupInfoResponseOfTitle)(AddressOf _service.GetAllTitles)
CallWebMethod(request, webMethodDeletgate)

您还可以从方法调用中删除(Of TResult, TResponse),因为这些可以从webMehtodDelegate

中确定