1

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, this 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 ?

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.