private static IDisposable WhenChanged <TProperty>(LambdaExpression propertyToListen, Action <TProperty> listener, object source) { var sp = ExpressionHelper.GetSourceAndPropertyName <object, TProperty>(source, propertyToListen); if (sp.Source is INotifyPropertyChanged npc) { var propertyName = sp.PropertyName; var disposable = EventBindingHandler <PropertyChangedEventHandler> .Create( (_, e) => { if (propertyName.Equals(e.PropertyName)) { if (npc.CanRead(e.PropertyName)) { listener?.Invoke((TProperty)npc.Read(e.PropertyName)); } } }, x => npc.PropertyChanged += x, x => npc.PropertyChanged -= x); return(disposable); } return(null); }
public void ObserverProperty_Test2() { var c1 = new Counter(); int ctr = 0; var ev = EventBindingHandler <PropertyChangedEventHandler> .Create(c1, "PropertyChanged", (_, e) => { ctr++; }); c1.Count++; ev.Dispose(); Assert.AreEqual(1, ctr); }