Skip to content

Instantly share code, notes, and snippets.

@rstropek
Created June 6, 2021 09:54
Show Gist options
  • Save rstropek/7b99f69d44d4ed4e9188f33ebd616a8a to your computer and use it in GitHub Desktop.
Save rstropek/7b99f69d44d4ed4e9188f33ebd616a8a to your computer and use it in GitHub Desktop.
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