ASP. net mvc ParameterDescriptor object details

Source: Internet
Author: User

This article introduces ASP. net mvc ParameterDescriptor objects in detail. If you are learning MVC ParameterDescriptor, you can see it.

Model binding is the process of preparing a parameter list for the method used as the target Action. Therefore, the parameter description is the core of Model binding. In the ASP. net mvc application programming interface, parameter metadata serving Model binding is represented by the ParameterDescriptor type, while the GetParameters method of ActionDescriptor returns a ParameterDescriptor array.

As shown in the following code snippet, ParameterDescriptor also implements the ICustomAttributeProvider interface to provide the features of the application on the corresponding parameters. The read-only attribute of ParameterDescriptor ActionDescriptor indicates the ActionDescriptor object that describes the Action method. ParameterName, ParameterType, and DefaultValue indicate the parameter name, type, and default value respectively.

The Code is as follows: Copy code

Public abstract class ParameterDescriptor: ICustomAttributeProvider
Public virtual object [] GetCustomAttributes (bool inherit );
Public virtual object [] GetCustomAttributes (Type attributeType, bool inherit );
Public virtual bool IsDefined (Type attributeType, bool inherit );

Public abstract ActionDescriptor {get ;}
Public abstract string ParameterName {get ;}
Public abstract Type ParameterType {get ;}
Public virtual object DefaultValue {get ;}

Public virtual ParameterBindingInfo BindingInfo {get ;}

The System. Web. Mvc. ParameterBindingInfo object in the read-only attribute BindingInfo of ParameterDescriptor encapsulates some information for controlling the binding behavior between request data and parameters. As shown in the following code snippet, the abstract class ParameterBindingInfo has four attributes. The ModelBinder object returned by the Binder attribute of the IModelBinder type is the core of the entire Model binding. We will introduce it separately in the subsequent sections of this chapter.

The Code is as follows: Copy code

Public abstract class ParameterBindingInfo
Public virtual IModelBinder Binder {get ;}

Public virtual ICollection <string> Include {get ;}
Public virtual ICollection <string> Exclude {get ;}
Public virtual string Prefix {get ;}

If the parameter type is a complex type, all its public read/write attributes are bound by default, the two ICollection <string> types Include and Exclude indicate the list of attribute names set to participate in/not to participate in binding. By default, the request data and parameters are bound by name strictly. However, sometimes the request data name has a Prefix, which is reflected in the Prefix attribute of ParameterBindingInfo.

Native ParameterBindingInfo is obtained through reflection of ParameterInfo, which represents the parameter. Such ParameterBindingInfo is represented by the ReflectedParameterDescriptor type. As shown in the following code snippet, The ParameterInfo object is represented by the read-only attribute ParameterInfo and initialized in the constructor.

The Code is as follows: Copy code

Public class ReflectedParameterDescriptor: ParameterDescriptor
Public ReflectedParameterDescriptor (ParameterInfo parameterInfo, ActionDescriptor actionDescriptor );
Public override object [] GetCustomAttributes (bool inherit );
Public override object [] GetCustomAttributes (Type attributeType, bool inherit );
Public override bool IsDefined (Type attributeType, bool inherit );

Public override ActionDescriptor {get ;}
Public override ParameterBindingInfo BindingInfo {get ;}
Public override object DefaultValue {get ;}
Public override string ParameterName {get ;}
Public override Type ParameterType {get ;}

Public ParameterInfo {get ;}

The BindingInfo attribute of ReflectedParameterDescriptor returns a ReflectedParameterBindingInfo object, which is an internal type. The Include, Exclude, and Prefix attributes of the BindingInfo are derived from the parsing of the bindattride feature used on the parameter. As shown in the following code snippet, bindattride also defines these three attributes. Include and Exclude are the attribute names that use commas as separators.

The Code is as follows: Copy code

[AttributeUsage (AttributeTargets. Parameter | AttributeTargets. Class, AllowMultiple = false, Inherited = true)]
Public sealed class BindAttribute: Attribute
Public bool IsPropertyAllowed (string propertyName );

Public string Include {get; set ;}
Public string Exclude {get; set ;}
Public string Prefix {get; set ;}

The IsPropertyAllowed method of the Boolean return type is used to determine whether the specified attribute can be bound. When the specified attribute name is in the Include list (or the Include list is empty) if the Exclude list is not displayed, True is returned. Otherwise, False is returned.

ParameterDescriptor is only one of the three important mvc objects. We will introduce the creation mechanisms of ControllerDescriptor, ActionDescriptor, ControllerDescriptor, and ActionDescriptor later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.