fork download
  1. public class LivetWeakEventListener<TDelegate, TEventArgs> where TEventArgs : EventArgs
  2. {
  3. private EventHandler<TEventArgs> _handler;
  4. private TDelegate _resultHandler;
  5. private Action<TDelegate> _remove;
  6.  
  7. private static void ReceiveEvent(WeakReference listenerWeakReference, object sender, TEventArgs args)
  8. {
  9. var listener = listenerWeakReference.Target as LivetWeakEventListener<TDelegate, TEventArgs>;
  10.  
  11. if (listener != null)
  12. {
  13. var handler = listener._handler;
  14.  
  15. if (handler != null)
  16. {
  17. handler(sender, args);
  18. }
  19. }
  20. }
  21.  
  22. private static TDelegate GetStaticHandler(WeakReference listenerWeakReference, Func<EventHandler<TEventArgs>, TDelegate> conversion)
  23. {
  24. return conversion((sender, e) => ReceiveEvent(listenerWeakReference, sender, e));
  25. }
  26.  
  27. private LivetWeakEventListener() { }
  28.  
  29. //コンストラクタ
  30. public LivetWeakEventListener(
  31. Func<EventHandler<TEventArgs>, TDelegate> conversion,
  32. Action<TDelegate> add,
  33. Action<TDelegate> remove,
  34. EventHandler<TEventArgs> handler)
  35. {
  36. _handler = handler;
  37. _remove = remove;
  38.  
  39. _resultHandler = GetStaticHandler(new WeakReference(this), conversion);
  40.  
  41. add(_resultHandler);
  42. }
  43.  
  44. //解放
  45. public void Release()
  46. {
  47. _remove(_resultHandler);
  48. }
  49.  
  50. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty