public static void TestJudge() { Action action = (Action)(EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EVar Int_1 = 2; EVar Int_2 = 2; EArray objectArray = EArray.CreateArraySpecifiedLength <object>(2); objectArray.StoreArray(0, Int_1.InStackAndPacket); objectArray.StoreArray(1, Int_2.InStackAndPacket); EJudge. If(Int_1 > 2)(() => { EVar str = "{0}>{1}"; methodInfoHelper.ExecuteMethod <string, object[]>("WriteLine", str, objectArray); }) .ElseIf(Int_1 == Int_2)(() => { EVar str = "{0}={1}"; methodInfoHelper.ExecuteMethod <string, object[]>("WriteLine", str, objectArray); }) .Else(() => { EVar str = "{0}<{1}"; methodInfoHelper.ExecuteMethod <string, object[]>("WriteLine", str, objectArray); }); }).Compile()); action(); }
/// <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 static void TestIf() { Delegate showResult = EHandler.CreateMethod <ENull>((il) => { EMethod method = typeof(Console); EVar emit_A = EVar.CreateWithoutTempVar(10); EVar emit_B = EVar.CreateVarFromObject(20); TestClass t = new TestClass() { Field = 10 }; t.PropertyName = "3"; EModel model = EModel.CreateModelFromObject(t); EJudge.If(emit_A == model.DLoadValue("Field").Operator)(() => { method.ExecuteMethod <string>("WriteLine", "相等"); }).ElseIf(emit_A > emit_B)(() => { method.ExecuteMethod <int>("WriteLine", emit_A); }).Else(() => { method.ExecuteMethod <int>("WriteLine", emit_B); }); EVar string_A = "6"; EVar string_B = "2"; EJudge.If(string_A == "1")(() => { method.ExecuteMethod <string>("WriteLine", string_A); }).ElseIf(string_A == model.DLoadValue("PropertyName").Operator)(() => { method.ExecuteMethod <string>("WriteLine", string_A); }).Else(() => { method.ExecuteMethod <string>("WriteLine", string_B); }); }).Compile(); ((Action)showResult)(); }
public static void TestDefault() { Action action = (Action)(EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EVar stringHandler = "16"; EVar intHandler = 10; EVar doubleHandler = 0.00; EJudge.If(EDefault.IsDefault(doubleHandler.TypeHandler, () => { doubleHandler.Load(); }))(() => { EMethod.Load(typeof(Console)).ExecuteMethod <string>("WriteLine", "doubleHandler是默认值"); }).Else(() => { doubleHandler.This(); methodInfoHelper.ExecuteMethod <double>("WriteLine"); }); }).Compile()); action(); }
public static void Create(Type TypeHandler) { Delegate func = EHandler.CreateMethod <object, bool>((il) => { LocalBuilder builder = il.DeclareLocal(TypeHandler); il.REmit(OpCodes.Ldarg_0); il.REmit(OpCodes.Unbox_Any, TypeHandler); il.REmit(OpCodes.Stloc_S, builder); EVar returnTrueResult = true; EVar returnFalseResult = false; EModel model = EModel.CreateModelFromBuilder(builder, TypeHandler); #region Property Dictionary <string, PropertyInfo> properties = model.Struction.Properties; foreach (var item in properties) { DebugHelper.WriteLine("检测" + item.Key + "是否为默认值:"); Type type = item.Value.PropertyType; if (type.IsValueType && type.IsPrimitive) { EJudge.If(EDefault.IsDefault(type, () => { model.LPropertyValue(item.Key); }))(() => { }).Else(() => { MethodHelper.ReturnValue(false); }); } else if (type.IsClass) { EJudge.If(ENull.IsNull(() => { model.LPropertyValue(item.Key); }))(() => { }).Else(() => { MethodHelper.ReturnValue(false); }); } } #endregion #region Fields Dictionary <string, FieldInfo> fields = model.Struction.Fields; foreach (var item in fields) { DebugHelper.WriteLine("检测" + item.Key + "是否为默认值:"); Type type = item.Value.FieldType; if (type.IsValueType && type.IsPrimitive) { EJudge.If(EDefault.IsDefault(type, model.DLoadValue(item.Key).DelayAction))(() => { }).Else(() => { MethodHelper.ReturnValue(false); }); } else if (type.IsClass) { EJudge.If(ENull.IsNull(model.DLoadValue(item.Key).DelayAction))(() => { }).Else(() => { MethodHelper.ReturnValue(false); }); } } #endregion il.EmitBoolean(true); }, "Check").Compile(typeof(CheckStructDelegate)); ClassCache.CheckStructDict[TypeHandler] = (CheckStructDelegate)func; }
/// <summary> /// 使用Natasha根据实例信息生成Command高速构建缓存,由参数类型以及SQL字符串决定缓存存储 /// </summary> /// <typeparam name="T">Command缓存方法中需要传入实例类型</typeparam> /// <param name="sql">SQL语句</param> /// <param name="value">实例</param> /// <returns>动态方法</returns> private static SqlDelegate <T> .GetGenericCommand GetEmitCommandGenericCache <T>(string sql, T value) { Type returnType = typeof(T); if (!Cache.SqlCache.ContainsKey(returnType)) { ModelAnalyser.Initialization(returnType); } Delegate newMethod = EHandler.CreateMethod <ERef <IDbCommand>, T, ENull>((il) => { EModel idbCommand = EModel.CreateModelFromParameter <IDbCommand>(0); idbCommand.UseRef(); idbCommand.Set("CommandText", sql); MatchCollection collection = ParameterRegex.Matches(sql); int i_length = collection.Count; if (i_length > 0) { Type type = value.GetType(); EModel valueDate = EModel.CreateModelFromParameter <T>(1); if (!Cache.SqlCache.ContainsKey(type)) { ModelAnalyser.Initialization(type); } EModel copyParameters = idbCommand.Load("Parameters"); IDictionary <string, Type> typeCache = Cache.StructionCache[type].ModelTypeCache; for (int i = 0; i < i_length; i += 1) { string memberName = collection[i].Groups[1].Value; Type tempType = typeCache[memberName]; copyParameters.Dup(); //+ Stack:[IDbCommand.Parameters] [IDbCommand.Parameters] EModel copyParameter = EMethod.Load(idbCommand).ExecuteMethod("CreateParameter").Dup(); //+ Stack:[IDbCommand.Parameters] [IDbCommand.Parameters] [IDbParameter] [IDbParameter] copyParameter.Set("ParameterName", "@".Append(memberName)); //+ Stack:[IDbCommand.Parameters] [IDbCommand.Parameters] [IDbParameter] Deal:([IDbParameter].ParameterName=@XXX) copyParameter.Dup().Set("DbType", (int)SqlTypes[tempType]); //+ Stack:[IDbCommand.Parameters] [IDbCommand.Parameters] [IDbParameter] Deal:([IDbParameter].DbType=XXX) copyParameter.Dup().Set("Value", () => { if (il.IsNullable(tempType)) { EJudge.If(valueDate.DLoad(memberName).DLoadValue("HasValue").DelayAction)(() => { valueDate.Load(memberName).LoadValue("Value").Packet(); }).Else(() => { EDBNull.LoadValue(); }); } else if (tempType.IsValueType) { valueDate.Load(memberName).Packet(); } else { EJudge.If(ENull.IsNull(valueDate.DLoad(memberName).DelayAction))(() => { EDBNull.LoadValue(); }).Else(() => { valueDate.Load(memberName).Packet(); }); } }); //+ Stack:[IDbCommand.Parameters] [IDbCommand.Parameters] [IDbParameter] Deal:([IDbParameter].Value=XXX) EMethod.Load <IList>().ExecuteMethod <object>("Add").Pop(); //+ Stack:[IDbCommand.Parameters] Deal:Add([IDbCommand.Parameters] [IDbParameter]) } copyParameters.Pop(); } }).Compile(typeof(SqlDelegate <T> .GetGenericCommand)); return((SqlDelegate <T> .GetGenericCommand)newMethod); }
/// <summary> /// 获取Reader映射缓存方法 /// </summary> /// <typeparam name="T">返回的类型</typeparam> /// <param name="reader">数据库返回的DataReader</param> /// <param name="sql">SQL语句</param> /// <returns>动态缓存方法</returns> private static SqlDelegate <T> .GetReaderInstance GetEmitReaderCache <T>(IDataReader reader, int startIndex, int length) { Type returnType = typeof(T); if (!Cache.SqlCache.ContainsKey(returnType)) { ModelAnalyser.Initialization(returnType); } SqlModel sqlModel = Cache.SqlCache[returnType]; Delegate dynamicMethod = EHandler.CreateMethod <IDataReader, T>((il) => { EMethod dataHandler = typeof(IDataRecord); EVar parameterVar = EVar.CreateVarFromParameter <IDataRecord>(0); if (returnType == typeof(object) || returnType == typeof(string) || returnType == typeof(byte[]) || (returnType.IsValueType && returnType.IsPrimitive) || il.IsNullable(returnType)) { if (returnType.IsValueType && returnType.IsPrimitive) { LoadStrongTypeValue(returnType, parameterVar, startIndex); } else { EJudge.If(() => { EMethod.Load(parameterVar).ExecuteMethod <int>("IsDBNull", startIndex); })(() => { if (il.IsNullable(returnType)) { EModel model = EModel.CreateModel(returnType).UseDefaultConstructor(); model.Load(); } else { ENull.LoadNull(); } }).Else(() => { LoadStrongTypeValue(returnType, parameterVar, startIndex); }); } } else { EModel model = EModel.CreateModel <T>().UseDefaultConstructor(); for (int i = startIndex; i < startIndex + length; i += 1) { string tempName = sqlModel.GetRealName(reader.GetName(i)); Type type = null; if (!model.Struction.Properties.ContainsKey(tempName) && !model.Struction.Fields.ContainsKey(tempName)) { continue; } else { type = sqlModel.Struction.ModelTypeCache[tempName]; } if (type.IsValueType && type.IsPrimitive) { model.Set(tempName, () => { LoadStrongTypeValue(type, parameterVar, i); }); } else { EJudge.IfTrue(() => { EMethod.Load(parameterVar).ExecuteMethod <int>("IsDBNull", i); })(() => { model.Set(tempName, () => { LoadStrongTypeValue(type, parameterVar, i); }); }); } } model.Load(); } }).Compile(typeof(SqlDelegate <T> .GetReaderInstance)); return((SqlDelegate <T> .GetReaderInstance)dynamicMethod); }