This project has moved. For the latest updates, please go here.

ObservableDynamicObject

.NET 4.0 Silverlight 4 Silverlight 5 Phone 7.0 Phone 7.1
S.png S.png S.png

Overview

ObservableDynamicObject wraps an object with a dynamic wrapper that converts normal properties, methods and events into observable sequences. It is dynamic because it derives from DynamicObject, which is the base class that enables use of the Dynamic Language Runtime (DLR) in .NET.

A dynamic object enables late binding so that members are resolved at runtime instead of at compile-time. The benefit of this behavior in ObservableDynamicObject is that you can wrap a POCO (plain old CLR object) that has members returning only scalar values and automatically convert them into the following kinds of observable sequences.
  • A method is converted into an asynchronous invocation that returns a hot observable sequence containing the return value. The sequence contains Unit when the original method returns void. The behavior is the same as the Observable.Start methods, without the IScheduler parameter.
  • A property is converted into an observable sequence of values. Subscribing to the sequence pushes the current value immediately and then pushes the latest value whenever the property changes. Not all properties are compatible. See the supported types and events list in the FromPropertyChangedPattern documentation for details.
  • An event is converted into an observable sequence of EventPattern<TEventArgs>, where TEventArgs is the strong type of the event's EventArgs.
All conversions take place at runtime, which means that there's no help from IntelliSense or the compiler. If you mistype a member's name or cast to an incorrect type, then you'll only find out when it throws an exception at runtime. Therefore, it's preferable in many cases to create explicit static members instead.

Use ObservableDynamicObject when you already have a dynamic reference to an object or when you have an existing class that cannot be modified and for which you need to convert several of its members into the kinds of observable sequences listed above. Also consider whether creating a static wrapper instead is a better solution in terms of maintainability and performance.

Make sure to thoroughly test for exceptions thrown at runtime caused by invalid casts, typographical errors, unavailable members and incompatible members.

"[Use] static typing where possible, dynamic typing where necessary". - Erik Meijer

Platform Support

ObservableDynamicObject is not supported on Windows Phone because the DLR is unavailable.

Lab

Download the labs application for your target platform and then run the Dynamic Observable lab. This lab contains working examples of ObservableDynamicObject. The source code for the lab is displayed in the application, for your convenience.

For more information, see Rxx Hands-on Labs.

Last edited Mar 10, 2012 at 4:31 PM by davedev, version 3