/// <summary> /// 使用Natasha根据参数数组信息生成Command高速构建缓存 /// </summary> /// <typeparam name="T">实际执行函数返回的类型</typeparam> /// <param name="sql">SQL语句</param> /// <param name="values">object参数数组</param> /// <returns>动态方法</returns> private static SqlDelegate <T> .GetCommandByObject GetEmitCommandCache <T>(string sql, object[] values) { Type returnType = typeof(T); if (!Cache.SqlCache.ContainsKey(returnType)) { ModelAnalyser.Initialization(returnType); } Delegate newMethod = EHandler.CreateMethod <ERef <IDbCommand>, object[], ENull>((il) => { EModel idbCommand = EModel.CreateModelFromParameter <IDbCommand>(0); idbCommand.UseRef(); idbCommand.Set("CommandText", sql); EArray arrayArg = EArray.CreateArrayFromParameter(1, typeof(object)); EModel copyParameters = idbCommand.Load("Parameters"); MatchCollection collection = ParameterRegex.Matches(sql); int length = collection.Count; for (int i = 0; i < length; i += 1) { Type type = values[i].GetType(); string memberName = collection[i].Groups[1].Value; copyParameters.Dup(); EModel copyParameter = EMethod.Load(idbCommand).ExecuteMethod("CreateParameter").Dup(); copyParameter.Set("ParameterName", "@".Append(memberName)); copyParameter.Dup().Set("DbType", (int)SqlTypes[type]); if (type.IsPrimitive) { copyParameter.Dup().Set("Value", () => { arrayArg.LoadArray(i); }); } else { EJudge.If(ENull.IsNull(() => { arrayArg.LoadArray(i); }))(() => { copyParameter.Dup().Set("Value", EDBNull.LoadValue); }).Else(() => { copyParameter.Dup().Set("Value", () => { arrayArg.LoadArray(i); }); }); } EMethod.Load <IList>().ExecuteMethod <object>("Add").Pop(); } copyParameters.Pop(); }).Compile(typeof(SqlDelegate <T> .GetCommandByObject)); return((SqlDelegate <T> .GetCommandByObject)newMethod); }
public void TestString() { string[] testArray = new string[] { "0", "1", "2", "3", "4", "5" }; for (int i = 0; i < testArray.Length; i += 1) { Delegate ShowDelegate = EHandler.CreateMethod <string>((il) => { EArray Model = testArray; Model.LoadArray(i); }).Compile(); Assert.Equal(i.ToString(), ((Func <string>)ShowDelegate)()); } }