See comments... I want the 2nd MapPost call to fail at compile time. I'm just experimenting with asp.net minimal pipeline. I think this requires constrained generics but I'm new to C# so thought someone could quickly answer this one (hopefully!).
var router = new ExampleRouter();
var controller = new ExampleWebController();
router.MapPost("/api/ProcessJob", controller.ProcessJob);
// I would like this to be a compile time error, instead of a runtime startup error.
router.MapPost("/api/ProcessJob2", controller.InvalidHandler);
class ExampleWebController {
public ExampleWebController() { }
public void InvalidHandler() { }
public bool ProcessJob(Job job) { return true; }
}
class ExampleRouter {
public ExampleRouter() { }
// TODO: Check at compile that `handler` takes a DTO and returns bool
public void MapPost<TFunc>(string path, TFunc handler) {
var func_type = typeof(TFunc);
// Prints "System.Func" for ProcessJob
// Prints "System.Action" for InvalidHandler
Console.WriteLine(func_type);
var args = func_type.GetGenericArguments();
foreach (Type arg in args) {
// Prints "Job", "System.Boolean" for ProcessJob...awesome
Console.WriteLine(arg);
}
// Construct the DTO dynamically.
// In reality, we'd do this when a http request comes in.
var job_type = args[0];
var job_obj = Activator.CreateInstance(job_type);
DTO dto = (DTO)job_obj;
// Prints "Hello I constructing from test body"
dto.construct_from_body("test body");
}
}
interface DTO {
public void construct_from_body(string body);
}
class Job : DTO
{
public Job() { }
public void construct_from_body(string body)
{
Console.WriteLine($"Hello I constructing from {body}");
}
}