samedi 21 juillet 2018

Accessing generic class parameter T from a method Attribute in ASP.NET Core

My Question

Is it possible to somehow access the type argument T of a generic class from a method attribute that is attached to a method in this generic class?

An Example

For example say I have a generic Controller MyBaseController handling a certain resource type T that is extended by a child controller MyChildController.

public class MyChildController : MyBaseController<BlogPost>
{
  // ...
}

Now I am using Swagger to document my API, more specifically Swashbuckle, hence, I am annotating my action with ProducesResponseType to specifiy the return type of the action. This where it get's complicated because ProducesResponseType expects a Type argument denoting the class of the value returned by the action.

public abstract class MyBaseController<T> : Controller where T : IResource
{
   [HttpGet, ProducesResponseType(T)] // <-- DOESN'T WORK!
   public async Task<IActionResult> doSomething()
   {
     // ...
   }
 }

In the example above I need T to somehow resolve to typeof(BlogPost)

Any ideas, or is this impossible?





Aucun commentaire:

Enregistrer un commentaire