public void TestMethod() { Delegate test = EHandler.CreateMethod <ulong>((il) => { EModel modelHandler = EModel.CreateModel <MethodStruct>().UseDefaultConstructor(); EMethod.Load(modelHandler).ExecuteMethod("GetULongMax"); }).Compile(); Func <ulong> action = (Func <ulong>)test; Assert.Equal(ulong.MaxValue, action()); test = EHandler.CreateMethod <ulong>((il) => { EModel modelHandler = EModel.CreateModel <MethodStruct>().UseDefaultConstructor(); EMethod.Load(modelHandler).ExecuteMethod("GetULongMin"); }).Compile(); action = (Func <ulong>)test; Assert.Equal(ulong.MinValue, action()); test = EHandler.CreateMethod <string>((il) => { EModel modelHandler = EModel.CreateModel <MethodStruct>().UseDefaultConstructor(); EVar param1 = "Hello"; EMethod.Load(modelHandler).ExecuteMethod <string, string>("GetString", param1, " World"); }).Compile(); Func <string> action1 = (Func <string>)test; Assert.Equal("Hello World", action1()); }
public static void TestCreateModel() { ClassBuilder builder = ClassBuilder.CreateModel("Hello"); builder.CreateDefaultConstructor(); builder.CreateField <string>("Age", FieldAttributes.Public); builder.CreateProperty <string>("Name"); builder.CreateMethod <ENull>("Show", MethodAttributes.Public, (classModel) => { classModel.SField("Age", "This is Age."); classModel.SProperty("Name", "This is name."); classModel.LPropertyValue("Name"); classModel.il.REmit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })); classModel.il.REmit(OpCodes.Ret); }); builder.EndBuilder(); Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EModel Model = EModel.CreateDynamicClass("Hello").UseDefaultConstructor(); EMethod.Load(Model).ExecuteMethod("Show"); Model.LFieldValue("Age"); Model.il.REmit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })); }).Compile(); ((Action)ShowDelegate)(); }
/// <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 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 void ShowInt() { //测试int上限 无临时变量 Delegate test = EHandler.CreateMethod <int>((il) => { EVar temp_var = int.MaxValue; EMethod.Load(typeof(Console)).ExecuteMethod <int>("WriteLine", temp_var); temp_var.Load(); }).Compile(); Func <int> action = (Func <int>)test; Assert.Equal(int.MaxValue, action()); //测试int下限 有临时变量 Delegate test2 = EHandler.CreateMethod <int>((il) => { EVar temp_var = EVar.CreateVarFromObject(int.MinValue); EMethod.Load(typeof(Console)).ExecuteMethod <int>("WriteLine", temp_var); temp_var.Load(); }).Compile(); Func <int> action2 = (Func <int>)test2; Assert.Equal(int.MinValue, action2()); }
public void ShowBoolean() { //测试float上限 无临时变量 Delegate test = EHandler.CreateMethod <bool>((il) => { EVar temp_var = true; EMethod.Load(typeof(Console)).ExecuteMethod <bool>("WriteLine", temp_var); temp_var.Load(); }).Compile(); Func <bool> action = (Func <bool>)test; Assert.Equal(true, action()); //测试float下限 有临时变量 Delegate test2 = EHandler.CreateMethod <bool>((il) => { EVar temp_var = EVar.CreateVarFromObject(false); EMethod.Load(typeof(Console)).ExecuteMethod <bool>("WriteLine", temp_var); temp_var.Load(); }).Compile(); Func <bool> action2 = (Func <bool>)test2; Assert.Equal(false, action2()); }
public void ShowChar() { //测试char 无临时变量 Delegate test = EHandler.CreateMethod <char>((il) => { EVar temp_var = 'a'; EMethod.Load(typeof(Console)).ExecuteMethod <char>("WriteLine", temp_var); temp_var.Load(); }).Compile(); Func <char> action = (Func <char>)test; Assert.Equal('a', action()); //测试string 有临时变量 Delegate test2 = EHandler.CreateMethod <char>((il) => { EVar temp_var = EVar.CreateVarFromObject('`'); EMethod.Load(typeof(Console)).ExecuteMethod <char>("WriteLine", temp_var); temp_var.Load(); }).Compile(); Func <char> action2 = (Func <char>)test2; Assert.Equal('`', action2()); }
public void ShowString() { //测试string 无临时变量 Delegate test = EHandler.CreateMethod <string>((il) => { EVar temp_var = string.Empty; EMethod.Load(typeof(Console)).ExecuteMethod <string>("WriteLine", temp_var); temp_var.Load(); }).Compile(); Func <string> action = (Func <string>)test; Assert.Equal(string.Empty, action()); //测试string 有临时变量 Delegate test2 = EHandler.CreateMethod <string>((il) => { EVar temp_var = EVar.CreateVarFromObject(string.Empty); EMethod.Load(typeof(Console)).ExecuteMethod <string>("WriteLine", temp_var); temp_var.Load(); }).Compile(); Func <string> action2 = (Func <string>)test2; Assert.Equal(string.Empty, action2()); }
public static void TestList() { List <string> list = new List <string>(); bool shut = list.All(func); Console.WriteLine(shut); Func <List <string>, Func <string, bool>, bool> ShowDelegate = (Func <List <string>, Func <string, bool>, bool>)EHandler.CreateMethod <List <string>, Func <string, bool>, bool>((il) => { EVar elist = EVar.CreateVarFromParameter(0); EVar efunc = EVar.CreateVarFromParameter(1); EMethod.Load(elist) .Use(typeof(Enumerable)) .AddGenricType <string>() .ExecuteMethod <Func <string, bool> >("All", efunc); }).Compile(); Console.WriteLine(ShowDelegate(list, (str) => { if (str == "1") { return(true); } return(false); })); Action ShowDelegate1 = (Action)EHandler.CreateMethod <ENull>((il) => { EModel model = EModel.CreateModel <Student>().UseDefaultConstructor(); EMethod.Load(model).ExecuteMethod("Show"); EMethod.Load(model).ExecuteMethod <string>("Show", "test"); }).Compile(); ((Action)ShowDelegate1)(); }
public void TestClass() { ComplexClassModel model = new ComplexClassModel(); model.FieldModel = new FieldStruct(); model.PropertyModel = new PropertyStruct(); model.MethodModel = new MethodStruct(); ComplexClassModel.Model = model; Delegate test = EHandler.CreateMethod <ComplexClassModel>((il) => { EVar ulongMinHandler = ulong.MinValue; EModel modelHandler = EModel.CreateModelFromObject(model); modelHandler.Load("FieldModel").Set("RefField", "Hello"); modelHandler.Load("PropertyModel").Set("RefProperty", "Hello"); modelHandler.Load("PropertyModel").Set("ValueProperty", ulongMinHandler); modelHandler.Load("Model").Load("FieldModel").Set("RefField", "Hello1"); modelHandler.Load("Model").Load("FieldModel").Set("ValueField", () => { EMethod.Load(modelHandler.DLoad("MethodModel").Operator).ExecuteMethod("GetULongMax"); }); modelHandler.Load(); }).Compile(); Func <ComplexClassModel> action = (Func <ComplexClassModel>)test; ComplexClassModel result = action(); Assert.Equal((ulong)0, result.FieldModel.ValueField); Assert.Equal(ulong.MinValue, result.PropertyModel.ValueProperty); Assert.Equal("Hello", result.FieldModel.RefField); Assert.Equal("Hello", result.PropertyModel.RefProperty); Assert.Equal(ulong.MaxValue, ComplexClassModel.Model.FieldModel.ValueField); Assert.Equal("Hello1", ComplexClassModel.Model.FieldModel.RefField); //Assert.Equal("Hello1", model.FieldModel.RefField); }
/// <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); }
private static bool LoadStrongTypeValue(Type type, EVar model, int Index) { string fastMethodName = "GetValue"; if (type == typeof(string)) { fastMethodName = "GetString"; } else if (type == typeof(int) || type == typeof(int?)) { fastMethodName = "GetInt32"; } else if (type == typeof(DateTime) || type == typeof(DateTime?)) { fastMethodName = "GetDateTime"; } else if (type == typeof(double) || type == typeof(double?)) { fastMethodName = "GetDouble"; } else if (type == typeof(bool) || type == typeof(double?)) { fastMethodName = "GetBoolean"; } else if (type == typeof(byte) || type == typeof(byte?)) { fastMethodName = "GetByte"; } else if (type == typeof(long) || type == typeof(long?)) { fastMethodName = "GetInt64"; } else if (type == typeof(Guid) || type == typeof(Guid?)) { fastMethodName = "GetGuid"; } else if (type == typeof(float) || type == typeof(float?)) { fastMethodName = "GetFloat"; } else if (type == typeof(char) || type == typeof(char?)) { fastMethodName = "GetChar"; } else if (type == typeof(short) || type == typeof(short?)) { fastMethodName = "GetInt16"; } else if (type == typeof(decimal) || type == typeof(decimal?)) { fastMethodName = "GetDecimal"; } else if (type == typeof(byte[])) { fastMethodName = "GetSqlBytes"; } EMethod.Load(model).ExecuteMethod <int>(fastMethodName, Index); if (fastMethodName != "GetValue") { return(false); } return(true); }
/// <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); }