How to automatically generating an extension method library of WPF Event -> IObservable

.net, c#, events, system.reactive, wpf

Solution

To get all the types deriving from FrameworkElement, you could use

var typesToDo = from t in Assembly.GetAssembly(typeof(FrameworkElement)).GetTypes()
                where t.IsSubclassOf(typeof(FrameworkElement)) 
                        && t.IsPublic 
                        && t.GetEvents().Any()
                select t;

and then you can use `type.GetEvents()` to get the events of each type. The EventInfo you get back will let you look at things like name, type, arguments etc.

You need to do a bit of extra work to cope with generic events, but it's not a huge amount.

I've put an example program up on GitHub along with an example of the output.

There is a risk that some of the output doesn't work properly, I haven't tried them all :) I did make sure they all build, and that at least some of them work correctly.

Problem

I almost exclusively use Reactive Extensions in my C# WPF apps these days. Adding and removing event handlers is an anti pattern and I'm completely jealous of the fact that F# events implement IObservable. To help C# developers the RX folks provide the below method ( and some others of varying type safeness ) ``` public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TDelegate, TEventArgs> ( Action<TDelegate> addHandler , Action<TDelegate> removeHandler ) where TEventArgs : EventArgs ``` http://msdn.microsoft.com/en-us/library/hh211731(v=vs.103).aspx I would use it like so ``` var movingEvents = Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(h => this.MouseMove += h, h => this.MouseMove -= h); ``` However this is tedious. What I'd like to be able to do is ``` var movingEvents = h.MouseMoveObserver(); ``` and be done with it. Such an extension method would look like ``` IObservable<MouseEventArgs> MouseMoveObserver(this Canvas This){ return Observable.FromEventPattern<MouseEventHandler, MouseEventArgs>(h => This.MouseMove += h, h => This.MouseMove -= h); } ``` It's not rocket science and I've been considering setting up a library where I add these extension methods one at a time as I need them. However I am sure some smart cookie could write a T4 template that processes all the controls in the WPF library via reflection and generates all the extension methods I would ever need. My question is ... Has anybody written such a code generator to map events to observables as above and if not would someone have any suggestions on how to do this? I'm not so good with regards to .Net reflection but some seed code might get me started.

Original source