public static void TestComplexClone() { TestClass testModel = new TestClass(); testModel.Name = "X"; testModel.Age = 10; TestStruct nestingStruct = new TestStruct(); nestingStruct.Age = 101; nestingStruct.Name = "CX"; TestInterfaceModel interfaceModel = new TestInterfaceModel(); interfaceModel.Shut = true; testModel.PInterface = interfaceModel; testModel.FieldNext = nestingStruct; testModel.PropertyNext1 = nestingStruct; Delegate ShowDelegate = EHandler.CreateMethod <TestClass>((il) => { EMethod method = typeof(Console); EModel classModel = EModel.CreateModelFromObject(testModel); classModel.ALoad("PrivatePName").GetAttributeModel("Attribute1").LoadValue("Name"); method.ExecuteMethod <string>("WriteLine"); classModel.Load(); }).Compile(); TestClass Result = ((Func <TestClass>)ShowDelegate)(); Console.WriteLine(Result.Name); Console.WriteLine(Result.FieldNext.Name); Console.WriteLine(Result.FieldNext.Age); Console.WriteLine(Result.PropertyNext1.Name); Console.WriteLine(Result.PropertyNext1.Age); Console.WriteLine(Result.PInterface.Shut); }
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">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); }
public static void DTestArray() { string[] strArray = new string[5]; int[] intArray = new int[5]; StructField[] structArray = new StructField[5]; ClassField[] classArray = new ClassField[5]; DocumentEnum[] enumArray = new DocumentEnum[5]; for (int i = 0; i < strArray.Length; i += 1) { strArray[i] = i.ToString(); intArray[i] = i; enumArray[i] = DocumentEnum.ID; structArray[i] = new StructField() { PublicAge = i }; classArray[i] = new ClassField() { PublicAge = i }; } //动态创建Action委托 Delegate newMethod = EHandler.CreateMethod <ENull>((il) => { EMethod method = typeof(Console); //从运行时获取数组并入栈到IL层临时变量 EArray stringArrayModel = strArray; ELoop.For(stringArrayModel, (currentElement) => { method.ExecuteMethod <string>("WriteLine", currentElement); }); EArray intArrayModel = intArray; ELoop.For(3, 5, 1, intArrayModel, (currentElement) => { method.ExecuteMethod <int>("WriteLine", currentElement); }); EArray arrayModel = EArray.CreateArraySpecifiedLength <int>(10); arrayModel.StoreArray(5, 6); arrayModel.StoreArray(6, 6); arrayModel.StoreArray(7, 6); ELoop.For(0, 10, 1, arrayModel, (currentElement) => { method.ExecuteMethod <int>("WriteLine", currentElement); }); //从运行时获取数组并入栈到IL层临时变量 EArray structArrayModel = EArray.CreateArrayFromRuntimeArray(structArray); ELoop.For(structArrayModel, (currentElement) => { EModel model = EModel.CreateModelFromAction(currentElement, typeof(StructField)); model.LFieldValue("PublicAge"); method.ExecuteMethod <int>("WriteLine"); }); EArray classArrayModel = EArray.CreateArrayFromRuntimeArray(classArray); ELoop.For(classArrayModel, (currentElement) => { EModel model = EModel.CreateModelFromAction(currentElement, typeof(ClassField)); model.LFieldValue("PublicAge"); method.ExecuteMethod <int>("WriteLine"); }); EArray enumArrayModel = EArray.CreateArrayFromRuntimeArray(enumArray); ELoop.For(enumArrayModel, (currentElement) => { EModel model = EModel.CreateModelFromAction(currentElement, typeof(DocumentEnum)); model.Load(); EPacket.Packet(typeof(DocumentEnum)); method.ExecuteMethod <object>("WriteLine"); }); }).Compile(); ((Action)newMethod)(); }