Skip to content

Commit c862e65

Browse files
Ryan Nowakrynowak
Ryan Nowak
authored andcommitted
Prepare to move CreateInferred
Part of: #11907
1 parent a74b627 commit c862e65

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ public RouteAttribute(string template) { }
419419
}
420420
public static partial class RuntimeHelpers
421421
{
422+
public static Microsoft.AspNetCore.Components.EventCallback<T> CreateInferredEventCallback<T>(object receiver, System.Action<T> callback, T value) { throw null; }
423+
public static Microsoft.AspNetCore.Components.EventCallback<T> CreateInferredEventCallback<T>(object receiver, System.Func<T, System.Threading.Tasks.Task> callback, T value) { throw null; }
422424
public static T TypeCheck<T>(T value) { throw null; }
423425
}
424426
public partial class UIChangeEventArgs : Microsoft.AspNetCore.Components.UIEventArgs

src/Components/Components/src/RuntimeHelpers.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Threading.Tasks;
56

67
namespace Microsoft.AspNetCore.Components
78
{
@@ -17,6 +18,43 @@ public static class RuntimeHelpers
1718
/// <typeparam name="T"></typeparam>
1819
/// <param name="value"></param>
1920
/// <returns></returns>
21+
//
22+
// This method is used to create an expression binding site with a compile-time known type. This helps with providing
23+
// good error messages, as well as proper method-group-to-delegate conversion when assigning component parameters.
2024
public static T TypeCheck<T>(T value) => value;
25+
26+
/// <summary>
27+
/// Not intended for use by application code.
28+
/// </summary>
29+
/// <param name="receiver"></param>
30+
/// <param name="callback"></param>
31+
/// <param name="value"></param>
32+
/// <returns></returns>
33+
//
34+
// This method is used with `@bind-Value` for components. When a component has a generic type, it's
35+
// really messy to write to try and write the parameter type for ValueChanged - because it can contain generic
36+
// type parameters. We're using a trick of type inference to generate the proper typing for the delegate
37+
// so that method-group-to-delegate conversion works.
38+
public static EventCallback<T> CreateInferredEventCallback<T>(object receiver, Action<T> callback, T value)
39+
{
40+
return EventCallback.Factory.Create<T>(receiver, callback);
41+
}
42+
43+
/// <summary>
44+
/// Not intended for use by application code.
45+
/// </summary>
46+
/// <param name="receiver"></param>
47+
/// <param name="callback"></param>
48+
/// <param name="value"></param>
49+
/// <returns></returns>
50+
//
51+
// This method is used with `@bind-Value` for components. When a component has a generic type, it's
52+
// really messy to write to try and write the parameter type for ValueChanged - because it can contain generic
53+
// type parameters. We're using a trick of type inference to generate the proper typing for the delegate
54+
// so that method-group-to-delegate conversion works.
55+
public static EventCallback<T> CreateInferredEventCallback<T>(object receiver, Func<T, Task> callback, T value)
56+
{
57+
return EventCallback.Factory.Create<T>(receiver, callback);
58+
}
2159
}
2260
}

0 commit comments

Comments
 (0)