Skip to content

Commit

Permalink
Fix EventCallback Net8.0 (#150)
Browse files Browse the repository at this point in the history
* feat: add early fix for EventCallback equality comparison

* feat: add correct types to acessors

* fix: commented out UnsafeAccessor for EventCallback<T>

* chore: Add explining comments

---------

Co-authored-by: Oleksandr Liakhevych <liakh.oleksandr32@gmail.com>
  • Loading branch information
Denny09310 and Dreamescaper authored Oct 27, 2024
1 parent 9da552b commit 1932978
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/BlazorBindings.Core/NativeControlComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;

namespace BlazorBindings.Core;
Expand Down Expand Up @@ -30,6 +31,19 @@ public virtual Task SetParametersAsync(ParameterView parameters)
return Task.CompletedTask;
}

// Equals methods are overridden only as a workaround to this issue:
// https://github.com/dotnet/aspnetcore/issues/53361
protected static bool Equals(EventCallback e1, object e2)
=> e2 is EventCallback other
&& ReferenceEquals(GetReceiver(ref e1), GetReceiver(ref other))
&& Equals(GetDelegate(ref e1), GetDelegate(ref other));

// TODO: uncomment on dotnet9.0
//protected static bool Equals<T>(EventCallback<T> e1, object e2)
// => e2 is EventCallback<T> other
// && ReferenceEquals(GetReceiver(ref e1), GetReceiver(ref other))
// && Equals(GetDelegate(ref e1), GetDelegate(ref other));

protected virtual void BuildRenderTree(RenderTreeBuilder builder)
{
if (_eventCallbackException != null)
Expand Down Expand Up @@ -165,4 +179,20 @@ void IComponent.Attach(RenderHandle renderHandle)
{
_renderHandle = renderHandle;
}


[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "Receiver")]
extern static ref IHandleEvent GetReceiver(ref EventCallback _this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "Delegate")]
extern static ref MulticastDelegate GetDelegate(ref EventCallback _this);

// Unfortunately can't use with generics https://github.com/dotnet/runtime/issues/89439
// TODO: uncomment on dotnet9.0

//[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "Receiver")]
//extern static ref IHandleEvent GetReceiver<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(ref EventCallback<T> _this);

//[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "Delegate")]
//extern static ref MulticastDelegate GetDelegate<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(ref EventCallback<T> _this);
}

0 comments on commit 1932978

Please sign in to comment.