我写了一个源生成器,里面有一个至关重要的工具方法:
private static global::Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapMethod
<[global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)] T>(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
[global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute("Route")] string pattern,
string httpMethod,
string method)
where T : notnull
{
var methodInfo = typeof(T).GetMethod(method) ?? throw new InvalidProgramException();
var request = global::Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(
methodInfo,
context => context.RequestServices.GetRequiredService<T>());
return endpoints
.MapMethods(pattern, [httpMethod], request.RequestDelegate)
.WithMetadata(methodInfo)
.WithMetadata([.. request.EndpointMetadata]);
}
2025/12/24小于 1 分钟