MobileBlazorBindings icon indicating copy to clipboard operation
MobileBlazorBindings copied to clipboard

Revisit ObjectExtensions.This() internal extension method

Open Eilon opened this issue 3 years ago • 4 comments
trafficstars

In https://github.com/dotnet/MobileBlazorBindings/pull/345 a new internal extension method was added to make creating more efficient delegates easier.

We should consider an approach that avoids extension methods on object, such as using a regular static method.

Eilon avatar May 27 '22 20:05 Eilon

Tricky thing here is that delegates, created for the same object, should be considered equal. That is possible with regular static method, but requires using reflection and is much slower (probably neglectable for our needs, but anyway):

https://sharplab.io/#v2:EYLgtghglgdgNAFxAJwK7wCYgNQB8ACATAIwCwAUBQG4TIAEAzgvQLx0BExhAzACwCs7ANwUAIgFMANsQAUTZAEoAdAFEAjqgiSGMidLnMFygJIwGAB3EBjBDIUjyewgcWqNWnU5dGlpi9dt7MSlubzdNbV0Q7xMzSxs7Bz1eMPUIzykU+R8/eMCHCnx+AB4Ae2AAKwCAPjo9WQB5SoC6UoU6Flq7DtrShyKy5ptaryaqm1b2zrpu6YAJKUtkJQAlcQBzVElaGTb+kvLxhBHosZa2npmBw5qFPQ2IBHElAGFkcUfxe/XPmQQAT0spQAZjJrkNjgo4K1oQCgaCFpIlsoAOLiBAAWXRAAtShgZOw1pttsh2EZ9oMjidJCkzhMLtNSkoACrYqAMAqUcj4bh0fDEABsfMIdERSwoAG8KHQZXzefyhTcJqz2X82QxWhDJpc+tLZTy+YLNUc6EStjslQhtYyHABfIA

Dreamescaper avatar May 28 '22 00:05 Dreamescaper

Interesting, that is definitely something to consider. I wonder if there's some clever syntax to avoid reflection and have the compiler provide the delegate with the right identity? It's just so unusual for extension methods to provide any concrete benefit aside from the compiler syntax sugar!

Eilon avatar May 28 '22 03:05 Eilon

Anyway, this is hardly a big matter, I just don't like extension methods on object 😁

Eilon avatar May 28 '22 03:05 Eilon

According to SharpLab, it compiles to smth like this:

    [CompilerGenerated]
    internal unsafe static Func<object> <<Main>$>g__Del4|0_3(object o)
    {
        return new Func<object>(o, (nint)(delegate*<object, object>)(&Helper.This));
    }

But I cannot write such code manually, I can't even find Func constructor with two parameters.

Dreamescaper avatar May 29 '22 13:05 Dreamescaper