title: How is Delegate being cast to RequestDelegate in ASP.NET Core?
body: <p>I looked into what <code>MapGet()</code> is doing and found that one of the overloads takes a <code>Delegate</code> type (class, not keyword, and not a <code>RequestDelegate</code>), but then pass it on to <code>MapMethods()</code>, which takes a <code>RequestDelegate</code>. What I find very confusing is how this cast takes place. A <code>RequestDelegate</code> has the signature <code>delegate Task RequestDelegate(HttpContext httpContext)</code>. When casting a delegate to another, don't they a) need the same signature, b) actually need to pass a reference to the <code>Invoke()</code> method of the delegate (that is to say you can't just convert a delegate to another despite their signatures matching, or so I read).</p>
  <p>Another thing I find extremely confusing is how one can pass in an arbitrary request handler to the Minimal API and it somehow being converted to a <code>RequestDelegate</code>. The documentation from Microsoft shows examples where the handler has no arguments like <code>() =&gt; ...</code>, and also arbitrary arguments which will be resolved through the dependency injection mechanism like <code>(MyClass a, HttpContext ctx, DbContext&lt;MyClass&gt; dbCtx) =&gt; ...</code>. I would assume this handler is somehow wrapped in another lambda that fits the <code>RequestDelegate</code> signature, so that ASP.NET Core can pass in a <code>HttpContext</code> and the wrapping lambda can then analyze the lambda passed in by the developer and decide how to resolve its depencies and such. But I have no clue at all where or how this happens. It literally just takes in a Delegate and passes it into another method which takes a <code>RequestDelegate</code>, and magically it's now a valid <code>RequestDelegate</code>?</p>
  <p>Anyone knows?</p>