I have a C# (.NET 4.6.1) project that uses a lot of events. I'm tempted to move all the event handlers to the new WeakEventManager pattern - to avoid endlessly worrying about deregistering handlers to avoid memory leaks.
However I have to do lots of testing for performance and want a way to switch between the two methods easily. So far I've been using conditional compilation to do this along the lines of :
#if WeakEvents
WeakEventManager<EventSource,EArgs>.AddHandler(source, "TheEvent", handler);
#else
source.TheEvent += handler;
#endif
This works, but its messy. Ideally I'd like to create a class that hides this away. ie create a class which internally can use either method. Then I can change all my source to attach handlers with the new class and switch easily (or even move to some new method in the future).
However I can't see how to write that class - because I can't pass the event as a parameter - and there would have to be some reflection going on with the handler/name which is beyond me.
Is there an easy way to do that?