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 NoErrorLoad(this ILGenerator il, object value, Type type) { if (il.IsNullable(type)) { if (value == null) { EModel model = EModel.CreateModel(type).UseDefaultConstructor(); model.Load(); } else { Action action = value as Action; if (action == null) { il.NoErrorLoad(value, type.GenericTypeArguments[0]); ConstructorInfo ctor = type.GetConstructor(new Type[] { type.GenericTypeArguments[0] }); il.REmit(OpCodes.Newobj, ctor); } else { action(); ConstructorInfo ctor = type.GetConstructor(new Type[] { type.GenericTypeArguments[0] }); il.REmit(OpCodes.Newobj, ctor); } } } else { il.NoErrorLoad(value); } }
public static void StaticClass() { Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EModel test = EModel.CreateModel <TestClass>().UseDefaultConstructor(); test.SField("NormalField", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", test.LoadValue("NormalField")); test.SField("StaticField", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", test.LoadValue("StaticField")); test.SField("Ref_StaticField", "10"); methodInfoHelper.ExecuteMethod <string>("WriteLine", test.LoadValue("Ref_StaticField")); test.SProperty("NormalProperty", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", test.LoadValue("NormalProperty")); test.SProperty("Ref_NormalProperty", "10"); methodInfoHelper.ExecuteMethod <string>("WriteLine", test.LoadValue("Ref_NormalProperty")); test.SProperty("StaticProperty", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", test.LoadValue("StaticProperty")); test.SProperty("Ref_StaticProperty", "10"); methodInfoHelper.ExecuteMethod <string>("WriteLine", test.LoadValue("Ref_StaticProperty")); }).Compile(); ((Action)ShowDelegate)(); }
public static void TestClassAndStruct() { //动态创建Action委托 Delegate newMethod = EHandler.CreateMethod <ENull>((il) => { EModel model = null; //测试类的字段 //model = EModel.CreateModel<ClassField>().UseDefaultConstructor(); //测试类的属性 //model = EModel.CreateModel<ClassProperty>().UseDefaultConstructor(); //测试结构体的字段 model = EModel.CreateModel <StructField>(); //测试结构体的属性 //model = EModel.CreateModel<StructProperty>(); model.Set("PublicName", "This is Public-Name"); model.Set("PrivateName", "This is Private-Name"); model.Set("PublicAge", 666); model.Set("PrivateAge", 666); EMethod method = typeof(Console); method.ExecuteMethod <string>("WriteLine", model.DLoadValue("PrivateName").DelayAction); method.ExecuteMethod <string>("WriteLine", model.LoadValue("PublicName")); method.ExecuteMethod <int>("WriteLine", model.LoadValue("PublicAge")); method.ExecuteMethod <int>("WriteLine", model.LoadValue("PrivateAge")); }).Compile(); ((Action)newMethod)(); }
public void TestProperty() { Delegate test = EHandler.CreateMethod <PropertyStruct>((il) => { EModel modelHandler = EModel.CreateModel <PropertyStruct>().UseDefaultConstructor(); modelHandler.Set("ValueProperty", ulong.MinValue); modelHandler.Set("StaticValueProperty", ulong.MaxValue); modelHandler.Set("RefProperty", "Hello World"); modelHandler.Load(); }).Compile(); Func <PropertyStruct> action = (Func <PropertyStruct>)test; PropertyStruct result = action(); Assert.Equal(ulong.MinValue, result.ValueProperty); Assert.Equal(ulong.MaxValue, PropertyStruct.StaticValueProperty); Assert.Equal("Hello World", result.RefProperty); }
public static void TestClass() { Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EModel model = EModel.CreateModel <TestClass>().UseDefaultConstructor(); model.Set("Name", "Name"); model.LoadValue("Name"); methodInfoHelper.ExecuteMethod <string>("WriteLine"); model.Set("Age", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.Load("FieldNext").LoadValue("Age")); }).Compile(); ((Action)ShowDelegate)(); }
public static void TestClassOperator() { Delegate showResult = EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EModel model = EModel.CreateModel <TestClass>().UseDefaultConstructor(); model.Set("Age", 10); EModel model2 = EModel.CreateModel <TestClass>().UseDefaultConstructor(); model2.Set("Age", 11); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") + model2.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") - model2.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") * model2.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") / model2.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") + 1000); }).Compile(); ((Action)showResult)(); }
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 static void TestNesting() { Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EModel structModel = EModel.CreateModel <TestStruct>().UseDefaultConstructor(); structModel.SProperty("Name", "Name1"); structModel.SField("Age", 101); EModel nesting_classModel = EModel.CreateModel <TestClass>().UseDefaultConstructor(); nesting_classModel.SProperty("Name", "Name2"); nesting_classModel.SField("Age", 102); EModel classModel = EModel.CreateModel <TestClass>().UseDefaultConstructor(); classModel.SProperty("Name", "Name"); classModel.SField("Age", 10); classModel.SField("FieldNext", structModel); classModel.SProperty("PropertyNext", nesting_classModel); classModel.LField("FieldNext").LFieldValue("Age"); methodInfoHelper.ExecuteMethod <int>("WriteLine"); classModel.LField("FieldNext").LPropertyValue("Name"); methodInfoHelper.ExecuteMethod <string>("WriteLine"); classModel.LPropertyValue("PropertyNext").LFieldValue("Age"); methodInfoHelper.ExecuteMethod <int>("WriteLine"); classModel.LPropertyValue("PropertyNext").LPropertyValue("Name"); methodInfoHelper.ExecuteMethod <string>("WriteLine"); }).Compile(); ((Action)ShowDelegate)(); }
public static void NoErrorLoad(this ILGenerator il, object value) { ENull enull = value as ENull; if (value != null) { Type type = value.GetType(); ILoadInstance instance = value as ILoadInstance; LocalBuilder builder = value as LocalBuilder; IOperator modelOperator = value as IOperator; if (value is Action) { ((Action)value)(); } else if (instance != null) { instance.Load(); } else if (builder != null) { il.EmitLoadBuidler(builder); } else if (enull != null) { il.REmit(OpCodes.Ldnull); } else if (value is Type) { il.REmit(OpCodes.Ldtoken, (Type)value); il.REmit(OpCodes.Call, ClassCache.ClassHandle); } else if (type.IsClass && type != typeof(string)) { EModel model = EModel.CreateModelFromObject(value, type); model.Load(); } else if (type.IsEnum) { il.REmit(OpCodes.Ldc_I4, (int)value); } else if (type.IsValueType && !type.IsPrimitive) { EModel model; if (EStruckCheck.IsDefaultStruct(value, type)) { model = EModel.CreateModel(type); } else { model = EModel.CreateModelFromObject(value, type); } model.Load(); } else { il.LoadObject(value, type); //il.CallNullableCtor(type); } } else { il.REmit(OpCodes.Ldnull); } }
public static void Main() { //ulong? i = null; //object t = i; //Console.WriteLine(t.GetType()); //Console.WriteLine(i.Value); ulong? i = null; string str = null; object t1 = str; //Console.WriteLine(model.ValueProperty.Value); ENatasha.Initialize(); //PropertyClass model = new PropertyClass(); //model.ValueProperty = ulong.MaxValue; //model.RefProperty = "Test"; //PropertyClass.StaticRefProeprty = "Static"; //PropertyClass.StaticValueProperty = ulong.MinValue; //Delegate test = EHandler.CreateMethod<PropertyClass>((il) => //{ // //EModel modelHandler = EModel.CreateModelFromObject(model); // EModel modelHandler = EModel.CreateModel<PropertyClass>().UseDefaultConstructor(); // modelHandler.Set("<StaticValueProperty>k__BackingField", ulong.MaxValue); // //modelHandler.Set("StaticValueProperty", ulong.MinValue); // //modelHandler.Set("RefProperty", "1"); // //modelHandler.Set("RefProperty", modelHandler.DLoad("RefProperty").Operator + modelHandler.DLoad("StaticRefProeprty").DelayAction); // modelHandler.Load(); //}).Compile(); //Func<PropertyClass> action = (Func<PropertyClass>)test; //PropertyClass result = action(); //T2(); //T<ulong?>(); //T<string>(); ClassWithNullableModel model = new ClassWithNullableModel(); model.ValueProperty = 11111; model.ValueField = 100; model.ValueProperty = null; ClassWithNullableModel.StaticValueField = null; ClassWithNullableModel.StaticValueProperty = 200; Delegate test = EHandler.CreateMethod <ulong?>((il) => { EModel modelHandler = EModel.CreateModel <ClassWithNullableModel>().UseDefaultConstructor(); modelHandler.Set("ValueProperty", (ulong)1); modelHandler.Load("PrivateProperty"); //modelHandler.Set("ValueProperty", modelHandler.DLoad("PrivateProperty").DLoad("Value").Operator + modelHandler.DLoad("StaticValueProperty").DLoad("Value").Operator); //EMethod.Load(typeof(Console)).ExecuteMethod<ulong?>("WriteLine", modelHandler.DLoad("PrivateProperty").DLoad("Value").DelayAction); // modelHandler.Set("ValueProperty", modelHandler.DLoad("PrivateProperty").DLoad("Value").Operator + modelHandler.DLoad("StaticValueProperty").DLoad("Value").Operator); //modelHandler.Set("StaticValueProperty", modelHandler.DLoad("ValueProperty").DLoad("Value").Operator + modelHandler.DLoad("StaticValueProperty").DLoad("Value").Operator); // modelHandler.Set("ValueField", modelHandler.DLoad("StaticValueProperty").DLoad("Value").Operator + modelHandler.DLoad("ValueField").DLoad("Value").Operator); //modelHandler.Load(); }).Compile(); Func <ulong?> action = (Func <ulong?>)test; //ClassWithNullableModel result = action(); ulong? obj = action(); object obj1 = typeof(ClassWithNullableModel).GetProperty("PrivateProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).GetValue(model); //ulong? a = (ulong?)obj1; DebugHelper.Close(); Console.ReadKey(); }
/// <summary> /// 使用Natasha根据实例信息生成Command高速构建缓存,由返回类型以及SQL字符串决定缓存存储 /// </summary> /// <typeparam name="T">实例类型</typeparam> /// <param name="sql">SQL语句</param> /// <param name="value">实例</param> /// <returns>动态方法</returns> private static SqlDelegate <T> .GetCommandByInstance GetEmitCommandCache <T>(string sql, object value) { 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); MatchCollection collection = ParameterRegex.Matches(sql); int i_length = collection.Count; if (i_length > 0) { Type type = value.GetType(); EModel valueArg = EModel.CreateModelFromParameter <T>(1); EModel valueDate = EModel.CreateModel(type); valueDate.Store(valueArg.InStackAndUnPacket); 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> .GetCommandByInstance)); return((SqlDelegate <T> .GetCommandByInstance)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); }
/// <summary> /// 深度复制对象,并压入栈中,创建一个新的临时变量 /// </summary> /// <param name="value">类或者结构体或者数组</param> /// <param name="type">类型</param> /// <returns></returns> public static LocalBuilder GetCloneBuilder(object value, Type type) { ILGenerator il = ThreadCache.GetIL(); if (il.IsNullable(type)) { LocalBuilder builder = il.DeclareLocal(type); if (value == null) { il.REmit(OpCodes.Ldloca_S, builder); il.InitObject(type); } else { il.LoadObject(value, value.GetType()); il.CallNullableCtor(type); } return(builder); } else if (value == null) { LocalBuilder builder = il.DeclareLocal(type); il.REmit(OpCodes.Ldnull); il.REmit(OpCodes.Stloc_S, builder.LocalIndex); return(builder); } else if (type.IsValueType && !type.IsPrimitive) { LocalBuilder builder = il.DeclareLocal(type); if (type.IsEnum) { il.EmitInt((int)value); il.REmit(OpCodes.Stloc_S, builder); return(builder); } if (EStruckCheck.IsDefaultStruct(value, type)) { EModel structModel = EModel.CreateModel(type).UseDefaultConstructor(); return(structModel.Builder); } } else if (type.IsArray) { Array tempArray = (Array)value; Type instanceType = value.GetType().GetElementType(); EArray array = EArray.CreateArraySpecifiedLength(value.GetType(), tempArray.Length); for (int i = 0; i < tempArray.Length; i += 1) { object result = tempArray.GetValue(i); if (result != null) { if (array.IsStruct && !array.ElementType.IsEnum) { if (EStruckCheck.IsDefaultStruct(result, instanceType)) { continue; } } array.StoreArray(i, result); } } return(array.Builder); } if (!EReflector.GetMethodDict.ContainsKey(type)) { EReflector.Create(type); } Dictionary <string, GetterDelegate> GetDict = EReflector.GetMethodDict[type]; ClassStruction struction = ClassCache.ClassInfoDict[type.Name]; EModel model = EModel.CreateModel(type).UseDefaultConstructor(); //foreach (var item in struction.Properties) //{ // MethodInfo info = item.Value.GetSetMethod(true); // if (info == null || GetDict[item.Key] == null || info.IsPrivate || info.IsStatic) // { // continue; // } // object result = GetDict[item.Key](value); // if (result == null) // { // continue; // } // model.SProperty(item.Key, result); //} foreach (var item in struction.Fields) { if (item.Value.IsStatic) { continue; } object result = GetDict[item.Key](value); if (result == null) { continue; } model.SField(item.Key, result); } return(model.Builder); }