简体   繁体   中英

Getting the properties of a parameter object

Hi I am just learning reflection and I am attempting to read the the parameters on the actions in controllers that are decorated with the attribute T4ValidateAttribute.

Lets take an example:

 public class LoginModelDTO
{      
    [Required(ErrorMessage = "Username is required")]
    [MaxLength(50, ErrorMessage = "Username should not have more then 50 chars")]
    [MinLength(25 , ErrorMessage = "Username should have at least 25 chars")]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Password is required")]
    [StringLength(25)]
    public string Password { get; set; }

    public bool RememberMe { get; set; }
}

[T4ValidateAttribute]
public bool LogIn(LoginModelDTO modelDTO)
{
    return m_loginService.Login(modelDTO);
}

My controllers are in a project called prokect.WebApi and my DTO's are in a project called project.DomainServices.Contracts. I will not be adding the ControllerInfo because it works if you guys think it's needed I will add it.

 //This code get all the controllers that inherit from the BaseApiController     
 List<Type> controllers = ControllersInfo.GetControllers<BaseApiController>("project.WebApi");

 foreach (var controller in controllers)
 {
    //This retrives a Dictionary that has the key the method name and the valie an array of ParameterInfo[]
     var actions = ControllersInfo.GetAllCustomActionsInController(controller, new T4ValidateAttribute());
     foreach (var parameterInfose in actions)
     {
         var parameters = parameterInfose.Value;
         foreach (var parameterInfo in parameters)
         {
            //This is where I do not knwo what to do   
         }
       }
    }

If you ooked at the code a bit and read the comments you can see that at this point I can access from each action it's parameters.

In our example the return parameter will be of type LoginModelDTO.

From here on I would like to itarate over all of the properties of this object for each property to get it's CustomAttributes.

How can I achieve this?

At the simplest level:

var attribs = Attribute.GetCustomAttributes(parameterInfo);

If all the attributes you are interested in have a common base type, you can restrict it to:

var attribs = Attribute.GetCustomAttributes(parameterInfo,
                    typeof(CommonBaseAttribute));

Then you just loop over attribs and pick what you care about. If you think there is at most one of a particular type:

SomeAttributeType attrib = (SomeAttributeType)Attribute.GetCustomAttribute(
       parameterInfo, typeof(SomeAttributeType));
if(attrib != null) {
     // ...
}

And finally, if you just want to know if the attribute is declared, this is much cheaper than GetCustomAttribute[s] :

if(Attribute.IsDefined(parameterInfo, typeof(SomeAttributeType))) {
     // ...
}

Note, however, that in your example the parameters do not have attributes .

See this SO question .

Basically they're using ReflectedControllerDescriptor to get list of ActionDescriptor instances (I think you are doing something similar in ControllersInfo.GetAllCustomActionsInController method):

var actionDescriptors = new ReflectedControllerDescriptor(GetType())
  .GetCanonicalActions();
foreach(var action in actionDescriptors)
{
   object[] attributes = action.GetCustomAttributes(false);

   // if you want to get your custom attribute only
   var t4Attributes = action.GetCustomAttributes(false)
    .Where(a => a is T4ValidateAttribute).ToList();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM