开发者

Assign event handlers to an object instance at runtime in a generic way

开发者 https://www.devze.com 2023-02-14 17:22 出处:网络
I\'m trying to create a generic method which assigns (and deassigns) an event handler to an object instance\'s event at runtime.

I'm trying to create a generic method which assigns (and deassigns) an event handler to an object instance's event at runtime.

Something like:

private void MyMethod<开发者_如何学Go;TEventArgs>(...)
   where TEventArgs : EventArgs
{
   EventHandler<TEventArgs> eventHandler = (s, e) =>
   {
      // logic
   }

   // want to assign eventHandler to an object instance passed into this method

   // more logic

   // want to deassign eventHandler from object instance
}

Currently, I'm passing in an assignEvent and deassignEvent delegate, something like:

private void MyMethod<TEventArgs>(Action<EventHandler<TEventArgs>> assignEvent, Action<EventHandler<TEventArgs>> deassignEvent)
{
   EventHandler<TEventArgs> eventHandler = (s, e) =>
   {
      // logic
   }

   assignEvent(eventHandler);

   // more logic

   deassignEvent(eventHandler);
}

And then calling the method like so:

var myObject = new MyObject();
MyMethod<MyEventArgs>(e => myObject.MyEvent += e, e => myObject.MyEvent -= e);

This just seems really horrible though!

What other options do I have?


A potentially better option here is to convert this to an IObservable<T>, via using Observable.FromEvent (in the Rx Framework). This allows you to treat the event itself in a first class manner, pass it into your method, then Dispose() the subscription at the end.


Unfortunately, there's not a much cleaner option. Events, despite being usable within the declaring class as an ordinary delegate member, are not publicly exposed as delegates. The event keyword means that it exposes add and remove functionality, but nothing else. In other words, you can't just pass myObject.MyEvent to MyMethod (unless you were to expose the actual delegate, which is a bad idea).

0

精彩评论

暂无评论...
验证码 换一张
取 消