I would like to create a delegate and a method that can be used to call any number of Web services that my application requires:
Example:
public DateCheckResponseGetDate(DateCheckRequest requestParameters) { delegate object WebMethodToCall(object methodObject); WebMethodToCall getTheDate = new WebMethodToCall(WebServices.GetTheDate); return (DateCheckResponse)CallWebMethod(getTheDate , requestParameters); } public TimeCheckResponse GetTime(TimeCheckRequest requestParameters) { delegate object WebMethodToCall(object methodObject); WebMethodToCall getTheTime = new WebMethodToCall(WebServices.GetTheTime); return (TimeCheckResponse)CallWebMethod(getTheTime, requestParameters); } private object CallWebMethod(WebMethodToCall method, object methodObject) { return method(methodObject); }
But, unfortunately, when I try to compile, I get these errors:
No overload for ‘GetTheDate’ matches delegate ‘WebMethodToCall’ No overload for ‘GetTheTime’ matches delegate ‘WebMethodToCall’
It seems like the delegate should work.
WebServices.GetTheDate and WebServices.GetTheTime both take a single parameter (DateCheckRequest and TimeCheckRequest, respectively) and both return a value.
So doesn’t the delegate match the signature of the two web methods? (both accept and return types derived from object).
Is it possible to use the object type to make a very reusable delegate in .NET 2.0?
I suggest you use a generic delegate such as
Func<T, TResult>:You’d then make
CallWebMethodgeneric too: