Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add statically-typed support for wrapping JS interop calls as C# delegates #61721

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

oroztocil
Copy link
Member

@oroztocil oroztocil commented Apr 28, 2025

This is a work-in-progress implementation of AsFunction extension methods for IJSObjectReference. These methods create C# delegates that can be used as typed wrappers for invoking referenced JS functions via interop. Currently, it adds overloads for void/value-returning functions with up to 3 arguments.

This is a reflection-free alternative to #61673.
The downside for this simpler implementation is the need to introduce multiple overloads into the public API.

/// <param name="jsObjectReference">The JavaScript object reference that represents the function to be invoked.</param>
/// <returns>A delegate that can be used to invoke the JavaScript function.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="jsObjectReference"/> is null.</exception>
public static Func<ValueTask> AsVoidFunction(this IJSObjectReference jsObjectReference)
Copy link
Member

@pavelsavara pavelsavara Apr 28, 2025

Choose a reason for hiding this comment

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

is guess ValueTask (vs Task) is here for consistency ?

Copy link
Member Author

@oroztocil oroztocil May 5, 2025

Choose a reason for hiding this comment

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

Yes, because the other Invoke* methods return ValueTask. The JSRuntime method which implements the actual call also returns ValueTask (although it creates it from a regular Task, for some reason).

@pavelsavara
Copy link
Member

pavelsavara commented Apr 28, 2025

How do I Dispose() the underlying function proxy if I only have the delegate now ?

Are we going to take the DX convenience to next level ? What about passing JS and C# functions as callbacks to other functions ?

@oroztocil
Copy link
Member Author

oroztocil commented May 5, 2025

How do I Dispose() the underlying function proxy if I only have the delegate now ?

To create the delegate, you must have had the IJSObjectReference returned from and interop call somewhere and you are responsible for disposing that just like you always were.

Are we going to take the DX convenience to next level ? What about passing JS and C# functions as callbacks to other functions ?

You can now pass JS functions (as IJSObjectReference) back to JS to use as callbacks.

There is no special support for passing C# functions. You have to create a JS wrapper and pass it the string identifiers of the assembly/method to call a static method, or passing a DotNetObjectReference + string identifier to call an instance method. I don't know if there is any demand for making this more streamlined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants