Skip to content

Better implementation of EventCallback.Equals #53395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/Components/Components/src/EventCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ public override int GetHashCode()
public override bool Equals(object? obj)
=> obj is EventCallback other
&& ReferenceEquals(Receiver, other.Receiver)
&& ReferenceEquals(Delegate, other.Delegate);
&& (Delegate?.Equals(other.Delegate) ?? (other.Delegate == null));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object.Equals(a, b) performs null handling and delegates to a.Equals(b).

}
2 changes: 1 addition & 1 deletion src/Components/Components/src/EventCallbackOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ public override int GetHashCode()
public override bool Equals(object? obj)
=> obj is EventCallback<TValue> other
&& ReferenceEquals(Receiver, other.Receiver)
&& ReferenceEquals(Delegate, other.Delegate);
&& (Delegate?.Equals(other.Delegate) ?? (other.Delegate == null));
}
36 changes: 36 additions & 0 deletions src/Components/Components/test/EventCallbackTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ await Assert.ThrowsAsync<ArgumentException>(() =>
});
}

[Fact]
public void EventCallbackOf_Equals_WhenANewDelegateIsCreated()
{
// Arrange
var component = new EventCountingComponent();

var delegate_1 = (EventArgs _) => { };
var delegate_2 = (MulticastDelegate)MulticastDelegate.CreateDelegate(typeof(Action<EventArgs>), delegate_1.Target, delegate_1.Method);
var eventcallback_1 = new EventCallback(component, delegate_1);
var eventcallback_2 = new EventCallback(component, delegate_2);

// Act
var result = eventcallback_1.Equals(eventcallback_2);

// Assert
Assert.True(result);
}

[Fact]
public async Task EventCallbackOfT_Action_Null()
{
Expand Down Expand Up @@ -415,6 +433,24 @@ public async Task EventCallbackOfT_FuncTTask_Arg()
Assert.Equal(1, component.Count);
}

[Fact]
public void EventCallbackOfT_Equals_WhenANewDelegateIsCreated()
{
// Arrange
var component = new EventCountingComponent();

var delegate_1 = (EventArgs _) => { };
var delegate_2 = (MulticastDelegate)MulticastDelegate.CreateDelegate(typeof(Action<EventArgs>), delegate_1.Target, delegate_1.Method);
var eventcallback_1 = new EventCallback<EventArgs>(component, delegate_1);
var eventcallback_2 = new EventCallback<EventArgs>(component, delegate_2);

// Act
var result = eventcallback_1.Equals(eventcallback_2);

// Assert
Assert.True(result);
}

private class EventCountingComponent : IComponent, IHandleEvent
{
public int Count;
Expand Down
Loading