Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,32 @@ static IObservable<int> ObserveAge<T>(T source)
where T : IHasAge
=> source.WhenValueChanged(source => source.Age);
}

// https://github.com/reactivemarbles/DynamicData/issues/1156
[Fact]
public void PropertyIsDeclaredOnInterfaceWithoutNotification_ChangesAreStillObserved()
{
var child = new NotifyingChildModel()
{
Age = 10
};

using var subscription = ObserveAge(child)
.RecordValues(out var results);

results.Error.Should().BeNull("no errors should have occurred");
results.RecordedValues.Should().ContainSingle("the initial value of the observed expression should have been published");
results.RecordedValues[0].Should().Be(child.Age, "the initial value of the observed expression should have been published");

++child.Age;

results.Error.Should().BeNull("no errors should have occurred");
results.RecordedValues.Skip(1).Should().ContainSingle("the value of the observed expression changed once, and the object notifies of it at runtime");
results.RecordedValues[1].Should().Be(child.Age, "the correct value should have been published");

static IObservable<int> ObserveAge<T>(T source)
where T : INotifyPropertyChanged, IHasPlainAge
=> source.WhenValueChanged(source => source.Age);
}
}
}
23 changes: 23 additions & 0 deletions src/DynamicData.Tests/Binding/WhenPropertyChangedFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ public double OtherAmount
public bool WasSubscribed { get; private set; }
}

private interface IHasPlainAge
{
int Age { get; }
}

private sealed class NotifyingChildModel
: IHasPlainAge, INotifyPropertyChanged
{
private int _age;

public event PropertyChangedEventHandler? PropertyChanged;

public int Age
{
get => _age;
set
{
_age = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Age)));
}
}
}

private interface IHasAge
: INotifyPropertyChanged
{
Expand Down
16 changes: 9 additions & 7 deletions src/DynamicData/Binding/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ internal static class ExpressionBuilder
{
internal static Func<object, IObservable<Unit>> CreatePropertyChangedFactory(this Expression source)
{
if ((source is not MemberExpression { Member: PropertyInfo property })
|| !typeof(INotifyPropertyChanged).IsAssignableFrom(property.DeclaringType))
if (source is not MemberExpression { Member: PropertyInfo property })
{
return static _ => Observable.Never<Unit>();
}

return target => Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
addHandler: handler => ((INotifyPropertyChanged)target).PropertyChanged += handler,
removeHandler: handler => ((INotifyPropertyChanged)target).PropertyChanged -= handler)
.Where(pattern => pattern.EventArgs.PropertyName == property.Name)
.Select(static _ => Unit.Default);
// The declaring type may not implement INotifyPropertyChanged (e.g. a plain interface), even though the target does at runtime.
return target => target is INotifyPropertyChanged notifier
? Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
addHandler: handler => notifier.PropertyChanged += handler,
removeHandler: handler => notifier.PropertyChanged -= handler)
.Where(pattern => pattern.EventArgs.PropertyName == property.Name)
.Select(static _ => Unit.Default)
: Observable.Never<Unit>();
}

internal static Func<object, object?> CreateInvoker(this Expression source)
Expand Down