📂ORM
2013-05-08 11:05阅读: 1916评论: 6推荐: 1

PetaPoco支持Dynamic(ExpandoObject)类型参数

修改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);
    }
}
复制代码

标红的部分就是修改过的代码

本文作者:拓荒者IT

本文链接:https://www.cnblogs.com/youring2/archive/2013/05/08/3066360.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

 

📌做了个微信公众号【拓荒者IT】,分享各种技术干货,新内容首发到公众号,欢迎关注❤️

posted @   拓荒者IT  阅读(1916)  评论(6编辑  收藏  举报
皮肤配置 参考地址:https://www.yuque.com/awescnb/user
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起