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】,分享各种技术干货,新内容首发到公众号,欢迎关注❤️
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步