首页 技术 正文
技术 2022年11月21日
0 收藏 461 点赞 2,218 浏览 4381 个字

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<ActionSelector> 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<ActionNameSelector> 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<string, object> 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 = ; 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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919