.NET MyMVC架構如何給方法賦值的教程

來源:互聯網
上載者:User
用過反射的人都知道,調用一個方法很簡單,但如何給一個【不知簽名】的方法準備傳入參數呢?
下面就來回答這個問題,請接著看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 字元串根本不能做任何轉換,所以直接返回null            return null;        try {            // 為了簡單,直接調用 .net framework中的方法。            // 如果轉換失敗,則會拋出異常。            return Convert.ChangeType(value, conversionType);        }        catch {            if( IsDebugMode )                throw;            // Debug 模式下拋異常            else                return null;    // Release模式下忽略異常(防止惡意使用者錯誤輸入)        }    }}

在給自訂的資料類型執行個體載入資料前,需要Crowdsourced Security Testing道這個執行個體對象有哪些屬性以及欄位,這個過程的代碼如下:

/// <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架構處理傳回值的教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.