Created
June 6, 2021 09:54
-
-
Save rstropek/7b99f69d44d4ed4e9188f33ebd616a8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
var app = new EndpointConventionBuilder(); | |
// Traditional way of defining a function with an attribute | |
[HttpGet("/")] int GetAnswer() => 42; | |
app.MapAction((Func<int>)GetAnswer); | |
// Now, we can remove the type cast: | |
app.MapAction(GetAnswer); | |
// We can even add attributes directly to lambdas: | |
app.MapAction([HttpGet("/")] () => 42); | |
// === | |
// Simulate presence of ASP.NET: | |
[AttributeUsage(AttributeTargets.Method)] | |
public class HttpGetAttribute : Attribute | |
{ | |
public HttpGetAttribute(string _) { /* ... */ } | |
} | |
interface IEndpointConventionBuilder { } | |
class EndpointConventionBuilder : IEndpointConventionBuilder { } | |
static class EndpointConventionBuilderExtensions | |
{ | |
public static void MapAction(this IEndpointConventionBuilder builder, Delegate action) { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment