开发者

Asp.Net Mvc - Find attributes for a given method

I'm trying to write some extensions for testing and one of the specs I want to run would be based on finding the list of attributes associated with a method (action).

So far, th开发者_开发百科is is the code I use to grab attributes off a method:

var homeController = new HomeController();
(controller.GetType().GetMethod(action, parameters)).GetCustomAttributes(false)

etc...

or for a controller, something like this (which is more explicit and not reusable)

(typeof(HomeController)).GetCustomAttributes(false)

So the question is, given I have a generic action of type ActionResult example:

var actionResult = new HomeController().Index();

Is there a way to find the attributes of that instance of the actionResult ?


If you just have actionResult then you don't really have the action method. You just have the result of its execution. Multiple methods can return the same result, for example.

You have to have a reference to the method itself. Your reflection calls should be fine. What do you not like about them?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜