PetaPoco支援Dynamic(ExpandoObject)型別參數

來源:互聯網
上載者:User

修改ParametersHelper類,添加ExpandoObject的類型判斷吧,修改後的代碼如下:

// PetaPoco - A Tiny ORMish thing for your POCO's.// Copyright  2011-2012 Topten Software.  All Rights Reserved. using System;using System.Collections.Generic;using System.Text.RegularExpressions;using System.Text;namespace PetaPoco.Internal{    internal static class ParametersHelper    {        // Helper to handle named parameters from object properties        public static string ProcessParams(string sql, object[] args_src, List<object> args_dest)        {            return rxParams.Replace(sql, m =>            {                string param = m.Value.Substring(1);                object arg_val;                int paramIndex;                if (int.TryParse(param, out paramIndex))                {                    // Numbered parameter                    if (paramIndex < 0 || paramIndex >= args_src.Length)                        throw new ArgumentOutOfRangeException(string.Format("Parameter '@{0}' specified but only {1} parameters supplied (in `{2}`)", paramIndex, args_src.Length, sql));                    arg_val = args_src[paramIndex];                }                else                {                    // Look for a property on one of the arguments with this name                    bool found = false;                    arg_val = null;                                foreach (var o in args_src)                    {                        //使其支援ExpandoObject                        if (o.GetType() == typeof(System.Dynamic.ExpandoObject))                        {                            var dic = o as IDictionary<string, object>;                            if (dic.ContainsKey(param))                            {                                arg_val = dic[param];                                found = true;                                break;                            }                        }                        else                        {                            var pi = o.GetType().GetProperty(param);                            if (pi != null)                            {                                arg_val = pi.GetValue(o, null);                                found = true;                                break;                            }                        }                    }                    if (!found)                        throw new ArgumentException(string.Format("Parameter '@{0}' specified but none of the passed arguments have a property with this name (in '{1}')", param, sql));                }                // Expand collections to parameter lists                if ((arg_val as System.Collections.IEnumerable) != null &&                    (arg_val as string) == null &&                    (arg_val as byte[]) == null)                {                    var sb = new StringBuilder();                    foreach (var i in arg_val as System.Collections.IEnumerable)                    {                        sb.Append((sb.Length == 0 ? "@" : ",@") + args_dest.Count.ToString());                        args_dest.Add(i);                    }                    return sb.ToString();                }                else                {                    args_dest.Add(arg_val);                    return "@" + (args_dest.Count - 1).ToString();                }            }            );        }        static Regex rxParams = new Regex(@"(?<!@)@\w+", RegexOptions.Compiled);    }}

標紅的部分就是修改過的代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.