It seems like I should have everything that I need here, but the particulars of making it happen are driving me crazy.
I have a static utility method which takes a web service client object, extracts a designated EventInfo from it, and is supposed to add some handlers to that event, essentially callbacks for when the web service invocation completes. The problem is, as any given event essentially has its own handler overload (the WCF generated code provides a unique SomeMethodCompletedEventArgs
for each method and corresponding event), I cannot figure out how to make this happen.
I have two handlers I want to attach, and the first one is just a lambda function:
(obj, args) => task.Complete()
So what I'd like to do is just this simple:
eventInfo.AddEventHandler(client, new EventHandler((obj, args) => task.Complete()));
This, however, generates a runtime InvalidCastException, because the eventInfo is expecting an EventHandler<SomeMethodCompletedEventArgs>
, not a plain EventHandler
. I believe this means that I need to dynamically create the EventHandler
delegate using eventInfo.EventHandlerType
somehow, but I have not figured out to combine that with the lambda function, or otherwise make a receiver that really does not care what particular flavor of EventArgs
is being used.
The only workaround that I've found is to create a generic template argument with which to pass in the particular event arguments type. This enables me to go:
eventInfo.AddEventHandler(client, new EventHandler<E>(...));
Where E
is the parameter in question. This is obviously clunky however, and it just seems wrong to have to pass this in when the extracted eventInfo
should tell us all we need to know.
It is worth noting that I am using a slightly constrained PCL Framework for Xamarin, which apparently does not include the static Delegate.CreateDelegate()
method that I've seen mentioned in related problems. I do have access to Activator
, though, which should cover most of the same bases.