This is probably a duplicate, but I can’t find the question I’m looking for, so I’m asking it.
How do you test that a method argument is decorated with an attribte? For example, the following MVC action method, using FluentValidation’s CustomizeValidatorAttribute:
[HttpPost]
[OutputCache(VaryByParam = "*", Duration = 1800)]
public virtual ActionResult ValidateSomeField(
[CustomizeValidator(Properties = "SomeField")] MyViewModel model)
{
// code
}
I’m sure I’ll have to use reflection, hopefully with strongly-typed lambdas. But not sure where to start.
Once you get a handle on the method with a
GetMethodInfocall via Reflection, you can simply callGetParameters()on that method, and then for each parameter, you can inspect theGetCustomAttributes()call for instances of type X. For example:This test, for example, would tell you if ANY of the parameters contained the attribute. If you wanted a specific parameter, you would need to change the
GetParameterscall into something more specific.