.net 有没有办法使泛型委托的使用更简单?
我有以下有效的方法:.net 有没有办法使泛型委托的使用更简单?,.net,vb.net,delegates,.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 TRe
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 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)
但是编译器不喜欢它
我想知道是否有人可以提供一种更干净的方法来调用该方法,而不必进行非常长的方法调用
提前感谢。使用
Imports wsLookupServiceProxy
在你班上的第一名,你可以把这件事写下来
Dim webMethodDeletgate As New WebMethodDelegate(Of RequestBase, GetSelectedLookupInfoResponseOfTitle)(AddressOf _service.GetAllTitles)
CallWebMethod(request, webMethodDeletgate)
您还可以从方法调用中删除
(TResult,treresponse)
,因为它们可以从webmehtedelegate
中确定。您是否关心CallWebMethod
或webMethodDelegate
谢谢,但我无法实现这一点。wsLookupServiceProxy是一个web服务,我不知道如何从WebMethodDelegate确定CallWebMethod类型,请提供一个示例。另外,CallWebMethod并不是返回TRespose类型的对象。我不确定您是否能够将其大大缩短。我使用了完整的名称空间(xxx.xxx.wsLookupServiceProxy),这允许我从中删除wsLookupServiceProxy。
Dim webMethodDeletgate As New WebMethodDelegate(Of RequestBase, GetSelectedLookupInfoResponseOfTitle)(AddressOf _service.GetAllTitles)
CallWebMethod(request, webMethodDeletgate)