public DynvokeMethod PrepTarget(string classname, string methodname, ParamProvider provider) { DynvokeMethod method = Get(classname, methodname); if (method == null) { return(null); } foreach (KeyValuePair <string, Type> entry in method.Parameters) { object value = null; if (provider.TryGetParam(entry.Key, entry.Value, out value)) { if (ParamReplacersExternal.ContainsKey(entry.Key) && ParamReplacersExternal[entry.Key].ExternalType == entry.Value) { ParamReplacmentContainer replCont = ParamReplacersExternal[entry.Key]; object newvalue = replCont.Replacer(value); Log.Debug(" Replacement: [" + replCont.InternalParamName + "] = (" + replCont.InternalType + ") [" + newvalue + "]"); method.SetParameter(replCont.InternalParamName, newvalue); } else { Log.Debug(" Parameter: [" + entry.Key + "] = (" + entry.Value + ") [" + value + "]"); method.SetParameter(entry.Key, value); } } } foreach (KeyValuePair <string, Type> entry in method.InjectedParameters) { object value = null; if (ParamInjectors.ContainsKey(entry.Key) && ParamInjectors[entry.Key].Type == entry.Value) { ParamInjectionContainer injCont = ParamInjectors[entry.Key]; value = injCont.Injector(); Log.Debug(" Injected: [" + entry.Key + "] = (" + entry.Value + ") [" + value + "]"); method.SetParameter(entry.Key, value); } } return(method); }
private static CommandCacheItem CreateCacheItem(string sql, CommandType commandType, object param) { if (param == null || string.IsNullOrEmpty(sql)) { return new CommandCacheItem { Sql = sql, Params = NullParamProvider } } ; // 参数可包含在属性或字段中,两种都要获取 var paramType = param.GetType(); var members = paramType .GetFields(BindingFlags.Instance | BindingFlags.Public) .Cast <MemberInfo>() .ToDictionary(x => x.Name, x => x); var props = paramType.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (var prop in props) { members.Add(prop.Name, prop); } var paramProvider = new ParamProvider(); if (commandType == CommandType.Text) { var patter = "[@:][a-zA-Z0-9_]+"; var matches = Regex.Matches(sql, patter); var existNames = new HashSet <string>(); foreach (Match match in matches) { var name = match.Value.Substring(1); // 避免重复处理 if (!existNames.Add(name)) { continue; } MemberInfo member; if (!members.TryGetValue(name, out member)) { continue; } paramProvider.AddDbParameterInfo(name, member); } } else { foreach (var member in members) { paramProvider.AddDbParameterInfo(member.Key, member.Value); } } return(new CommandCacheItem { Sql = sql, Params = paramProvider.GenerateParameters }); }