下面就来回答这个问题,请接着看getactioncallparameters的实现过程:
private static object[] getactioncallparameters(httpcontext context, actiondescription action) { if( action.parameters == null || action.parameters.length == 0 ) return null; object[] parameters = new object[action.parameters.length]; for( int i = 0; i < action.parameters.length; i++ ) { parameterinfo p = action.parameters[i]; if( p.isout ) continue; if( p.parametertype == typeof(namevaluecollection) ) { if( string.compare(p.name, "form", stringcomparison.ordinalignorecase) == 0 ) parameters[i] = context.request.form; else if( string.compare(p.name, "querystring", stringcomparison.ordinalignorecase) == 0 ) parameters[i] = context.request.querystring; else if( string.compare(p.name, "headers", stringcomparison.ordinalignorecase) == 0 ) parameters[i] = context.request.headers; else if( string.compare(p.name, "servervariables", stringcomparison.ordinalignorecase) == 0 ) parameters[i] = context.request.servervariables; } else{ type paramtertype = p.parametertype.getrealtype(); // 如果参数是简单类型,则直接从httprequest中读取并赋值 if( paramtertype.issimpletype() ) { object val = modelhelper.getvaluebykeyandtypefrommrequest( context.request, p.name, paramtertype, null); if( val != null ) parameters[i] = val; } else { // 自定义的类型。首先创建实例,然后给所有成员赋值。 // 注意:这里不支持嵌套类型的自定义类型。 object item = activator.createinstance(paramtertype); modelhelper.fillmodel(context.request, item, p.name); parameters[i] = item; } } } return parameters; }
要理解这段代码还要从前面的【查找action的过程】说起,在那个阶段,可以获取一个action的描述,具体在框架内部表示为actiondescription类型:
internal sealed class actiondescription : basedescription{ public controllerdescription pagecontroller; //为pageaction保留 public methodinfo methodinfo { get; private set; } public actionattribute attr { get; private set; } public parameterinfo[] parameters { get; private set; } public bool hasreturn { get; private set; } public actiondescription(methodinfo m, actionattribute atrr) : base(m) { this.methodinfo = m; this.attr = atrr; this.parameters = m.getparameters(); this.hasreturn = m.returntype != reflectionhelper.voidtype; } }
在构造函数的第三行代码中,我就可以得到这个方法的所有参数情况。
然后,我在就可以在getactioncallparameters方法中,循环每个参数的定义,为它们赋值。
这段代码也解释了前面所说的只支持4种namevaluecollection集合的原因。
注意了,我在获取每个参数的类型时,是使用了下面的语句:
type paramtertype = p.parametertype.getrealtype();
实际上,parametertype就已经反映了参数的类型,为什么不直接使用它呢?
答:因为【可空泛型】的原因。这个类型我们需要特殊的处理。
例如:如果某个参数是这样声明的: int? id
那么,即使在querystring中包含id这样一个参数,我也不能直接转成 int? 使用这种类型,必须得到它的【实际类型】。
getrealtype()是个扩展方法,它就专门完成这个功能:
/// <summary> /// 得到一个实际的类型(排除nullable类型的影响)。比如:int? 最后将得到int/// </summary> /// <param name="type"></param> /// <returns></returns>public static type getrealtype(this type type) { if( type.isgenerictype ) return nullable.getunderlyingtype(type) type; else return type; }
如果某个参数的类型是一个自定义的类型,框架会先创建实例(调用无参的构造函数),然后给它的property, field赋值。
注意了:自定义的类型,一定要提供一个无参的构造函数。
为自定义类型的实例填充数据成员的代码如下:
internal static class modelhelper{ public static readonly bool isdebugmode; static modelhelper() { compilationsection configsection = configurationmanager.getsection("system.web/compilation") as compilationsection; if( configsection != null ) isdebugmode = configsection.debug; } /// <summary> /// 根据httprequest填充一个数据实体。 /// 这里不支持嵌套类型的数据实体,且要求各数据成员都是简单的数据类型。 /// </summary> /// <param name="request"></param> /// <param name="model"></param> public static void fillmodel(httprequest request, object model, string paramname) { modeldescripton descripton = reflectionhelper.getmodeldescripton(model.gettype()); object val = null; foreach( datamember field in descripton.fields ) { // 这里的实现方式不支持嵌套类型的数据实体。 // 如果有这方面的需求,可以将这里改成递归的嵌套调用。 val = getvaluebykeyandtypefrommrequest( request, field.name, field.type.getrealtype(), paramname); if( val != null ) field.setvalue(model, val); } } /// <summary> /// 读取一个http参数值。这里只读取querystring以及form /// </summary> /// <param name="request"></param> /// <param name="key"></param> /// <returns></returns> public static string gethttpvalue(httprequest request, string key) { string val = request.querystring[key]; if( val == null ) val = request.form[key]; return val; } public static object getvaluebykeyandtypefrommrequest( httprequest request, string key, type type, string paramname) { // 不支持复杂类型 if( type.issimpletype() == false ) return null; string val = gethttpvalue(request, key); if( val == null ) { // 再试一次。有可能是多个自定义类型,form表单元素采用变量名做为前缀。 if( string.isnullorempty(paramname) == false ) { val = gethttpvalue(request, paramname + "." + key); } if( val == null ) return null; } return safechangetype(val.trim(), type); } public static object safechangetype(string value, type conversiontype) { if( conversiontype == typeof(string) ) return value; if( value == null || value.length == 0 ) // 空字符串根本不能做任何转换,所以直接返回null return null; try { // 为了简单,直接调用 .net framework中的方法。 // 如果转换失败,则会抛出异常。 return convert.changetype(value, conversiontype); } catch { if( isdebugmode ) throw; // debug 模式下抛异常 else return null; // release模式下忽略异常(防止恶意用户错误输入) } } }
在给自定义的数据类型实例加载数据前,需要先知道这个实例对象有哪些属性以及字段,这个过程的代码如下:
/// <summary> /// 返回一个实体类型的描述信息(全部属性及字段)。/// </summary> /// <param name="type"></param> /// <returns></returns>public static modeldescripton getmodeldescripton(type type) { if( type == null ) throw new argumentnullexception("type"); string key = type.fullname; modeldescripton mm = (modeldescripton)s_modeltable[key]; if( mm == null ) { list<datamember> list = new list<datamember>(); (from p in type.getproperties(bindingflags.instance | bindingflags.public) select new propertymember(p)).tolist().foreach(x=>list.add(x)); (from f in type.getfields(bindingflags.instance | bindingflags.public) select new fieldmember(f)).tolist().foreach(x => list.add(x)); mm = new modeldescripton { fields = list.toarray() }; s_modeltable[key] = mm; } return mm; }
在拿到一个类型的所有属性以及字段的描述信息后,就可以通过循环的方式,根据这些数据成员的名字去querystring,form读取所需的数据了。
【相关推荐】
1. 特别推荐:“php程序员工具箱”v0.1版本下载
2. asp免费视频教程
3. 入门级的.net mvc 实例
4. mymvc框查找action的过程详解
5. .net mymvc框架执行action的过程详解
6. .net mymvc框架处理返回值的教程
以上就是.net mymvc框架如何给方法赋值的教程的详细内容。
