jeudi 11 août 2016

Get the result object types from an action in MVC using reflection

I would to retrieve the the result object type of each action.

Generally, the structure of an action in my project is this:

[ServiceControllerResult(typeof(MyControllerResult))]
public ActionResult MyMethod(MyControllerRequest request)
{
    var response = new ServiceResponse<MyControllerResult>();
    // do something
    return ServiceResult(response);
}

Now, how I can get the MyControllerResult object type by reflection?

I write this code but I don't know how to retrieve the object type and the object Name:

var attributes = method.GetCustomAttributes(); // list of attributes
var resultAttribute =  attributes.Where(x => x.ToString().Contains("ServiceControllerResultAttribute")).FirstOrDefault();

P.S. I write the Contains method to retrieve the attribute because the decorator ServiceControllerResult is optional.

Thanks





Aucun commentaire:

Enregistrer un commentaire