温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

.NET MyMVC框架给方法赋值的案例

发布时间:2020-08-28 14:03:38 来源:亿速云 阅读:100 作者:小新 栏目:编程语言

这篇文章主要介绍.NET MyMVC框架给方法赋值的案例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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读取所需的数据了。

以上是.NET MyMVC框架给方法赋值的案例的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ne
AI