博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ActionDescriptor 的认识
阅读量:6983 次
发布时间:2019-06-27

本文共 4733 字,大约阅读时间需要 15 分钟。

     ActionDescriptor的作用是对Action方法的元数据的描述,通过ActionDescriptor我们可以获取到action方法的相关的名称,所属控制器,方法的参数列表,应用到方法上的特性以及一些筛选器;ActionDescriptor是由ControllerDescriptor类中的FindAction方法进行创建;

     ActionDescriptor类也继承了ICustomAttributeProvider接口,所以ActionDescriptor类或是它的继承类也实现了GetCustomAttributes和IsDefined方法;

     ActionDescriptor类中的属性和ControllerDescriptor类的属性差不多,包含有一个含有描述操作符唯一性ID的 UniqueId,表示方法名称的ActionName以及action所属于的控制器的元数据描述类ControllerDescriptor等属性字段;为了加快action方法的执行效率,ActionDescriptor类内部还创建了一个action方法调度的缓存属性(ActionMethodDispatcherCache )DispatcherCache;

     ActionMethodDispatcherCache 这个类结构是key为MethodInfo value 为ActionMethodDispatcher的字典缓存,在这个缓存类中通过GetDispatcher方法来快速获取ActionMethodDispatcher类;

  ReflectedActionDescriptor

      ReflectedActionDescriptor类在MVC框架中继承了ActionDescriptor类而且继承了IMethodInfoActionDescriptor接口(获取MethodInfo信息),并且覆盖了一些父类的方法;

     在ReflectedActionDescriptor类的构造函数中除了一些基本属性的赋值以外,还会内部调用VerifyActionMethodIsCallable方法来对methodInfo属性进行验证,

     VerifyActionMethodIsCallable方法的验证逻辑:

     1.方法不是静态函数

     2.方法的名称不能是ControllerBase类中的方法

     3.泛型方法中不能包含未赋值的泛型类型参数

     4.方法的参数中不能有in 或是out修饰的参数

    如果验证不通过的话,直接throw一个ArgumentException异常;

    在ReflectedActionDescriptor类中包含有一个GetFilterAttributes方法来获取应用到action方法上的FilterAttribute的特性列表;

    对于action方法中的参数的元数据的获取是通过GetParameters方法,在ReflectedActionDescriptor类中有一个ParameterDescriptor[]的数组缓存,当缓存中存在时直接从缓存数组中获取相应的参数元数据信息,如果没有则通过MethodInfo的GetParameters方法获取,然后调用ReflectedParameterDescriptor类的构造函数创建参数的元数据信息;

    在Control可以存在同名的action方法,当时同名的action方法不能有相同的请求方式,我们可以标记一个action方式支持Post,Get等提交方式,在MVC框架中HttpGetAttribute,HttpPostAttribute等特性类都继承了抽象类ActionMethodSelectorAttribute类,在ActionMethodSelectorAttribute类中只包含一个IsValidForRequest抽象方法

  

public abstract class ActionMethodSelectorAttribute : Attribute    {      public abstract bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo);    }

     在IsValidForRequest 方法目的是验证当前action的方法是否与当前的请求类型相匹配;

     在ReflectedActionDescriptor类中提供了获取这些筛选特性的方法GetSelectors,这个方法内部会返回作用于当前action当前的ActionMethodSelectorAttribute类的子类的列表,由于这个返回值是ActionSelector类型的集合,而ActionSelector是一个参数为ControllerContext返回值为布尔类型的委托

public static ICollection
GetSelectors(MethodInfo methodInfo) { ActionMethodSelectorAttribute[] attrs = (ActionMethodSelectorAttribute[])methodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), inherit: true); ActionSelector[] selectors = Array.ConvertAll(attrs, attr => (ActionSelector)(controllerContext => attr.IsValidForRequest(controllerContext, methodInfo))); return selectors; }

    在ReflectedActionDescriptor类中还包含一个和GetSelectors方法类似的内部方法 GetNameSelectors,这个方法返回值为ActionNameSelector类型,这个类型也是一个委托类型,方法的作用是筛选ActionNameSelectorAttribute抽象类的子类的列表;其实内部实现和GetNameSelectors是相似的

 

public static ICollection
GetNameSelectors(MethodInfo methodInfo) { ActionNameSelectorAttribute[] attrs = (ActionNameSelectorAttribute[])methodInfo.GetCustomAttributes(typeof(ActionNameSelectorAttribute), inherit: true); ActionNameSelector[] selectors = Array.ConvertAll(attrs, attr => (ActionNameSelector)((controllerContext, actionName) => attr.IsValidName(controllerContext, actionName, methodInfo))); return selectors; }

      对于ActionNameSelectorAttribute与ActionMethodSelectorAttribute类的区别是前者是对action的名字进行筛选,而后者是对请求方式的筛选;

      当获得了ReflectedActionDescriptor类后就会执行action方法的执行,对于action方法的执行时直接调用类的Execute方法;

        public override object Execute(ControllerContext controllerContext, IDictionary<string, object> parameters)

       在Execute方法的内部,由于考虑到性能,避免使用Linq或是委托;

      首先通过MethodInfo.GetParameters获取到参数信息列表,然后对参数列表进行遍历验证,参数验证保证不能有重复的参数名,如果参数值为空的话要保证参数的类型是可以为空,当参数值不为空时,要保证参数值的类型和参数的类型一致;如果其中一条规则不符合时就会    throw ArgumentException 异常;

     当参数列表遍历完成后,就会在ActionMethodDispatcherCache缓存中通过GetDispatcher方法获取到ActionMethodDispatcher类,然后调用ActionMethodDispatcher类来进行方法的调用,调用完成后执行结果返回;

 

public override object Execute(ControllerContext controllerContext, IDictionary
parameters){ if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (parameters == null) { throw new ArgumentNullException("parameters"); }// Performance sensitive so avoid Linq or delegates. ParameterInfo[] parameterInfos = MethodInfo.GetParameters(); object[] parametersArray = new object[parameterInfos.Length]; for (int i = 0; i < parameterInfos.Length; i++) { ParameterInfo parameterInfo = parameterInfos[i]; object parameter = ExtractParameterFromDictionary(parameterInfo, parameters, MethodInfo); parametersArray[i] = parameter; } ActionMethodDispatcher dispatcher = DispatcherCache.GetDispatcher(MethodInfo); object actionReturnValue = dispatcher.Execute(controllerContext.Controller, parametersArray); return actionReturnValue;}

 

     

     

 

转载于:https://www.cnblogs.com/h20064528/p/5049052.html

你可能感兴趣的文章
Java并发(具体实例)——几个例子
查看>>
【待补】java开发Web Service
查看>>
两个有用的数组扩展方法
查看>>
英语发音规则---H字母
查看>>
js进阶 10-11/12 表单伪类选择器的作用
查看>>
C#中Invoke的用法
查看>>
pxe无人值守安装操作系统
查看>>
UESTC 2014 Summer Training #11 Div.2
查看>>
[笔记] SDRAM读写控制
查看>>
size_t的定义
查看>>
mybatis 模糊查询 like的三种方式
查看>>
VML相关
查看>>
HDU 1051 - Rightmost Digit
查看>>
5_2 实现过程中
查看>>
1035. 插入与归并(25)
查看>>
第二周进度总结
查看>>
JavaScript 精粹
查看>>
Android组件化和插件化开发
查看>>
远程更改ESXi主机IP
查看>>
【java】 虹软ArcFace 2.0 人脸信息识别(年龄、性别)
查看>>