private static LocalBuilder MapToEntity(this EmitBasic basic, Object instance, Type type) { if (instance == null) { ManagerGX.ShowEx("entity is not null!"); } var ctor = type.GetConstructor(Type.EmptyTypes); if (!ctor.IsPublic) { ManagerGX.ShowEx("type need ctor public!"); } var name = type.FullName; LocalBuilder model = basic.DeclareLocal(type); basic.Emit(OpCodes.Newobj, type); basic.Emit(OpCodes.Stloc_S, model); FastProperty[] emits = type.CachePropsManager(); for (int i = 0; i < emits.Length; i++) { var propValue = emits[i].Get(instance); if (propValue == null) { continue; } basic.Emit(OpCodes.Ldloc_S, model); basic.EmitValue(propValue, emits[i].PropertyType); basic.Emit(OpCodes.Callvirt, emits[i].SetMethod); } return(model); }
internal static void PushArray(this EmitBasic basic, Type type) { if (type == typeof(String)) { basic.Emit(OpCodes.Stelem_Ref); } else if (type == typeof(Boolean)) { basic.Emit(OpCodes.Stelem_I1); } else if (type == typeof(SByte)) { basic.Emit(OpCodes.Stelem_I1); } else if (type == typeof(Byte)) { basic.Emit(OpCodes.Stelem_I1); } else if (type == typeof(Int16)) { basic.Emit(OpCodes.Stelem_I2); } else if (type == typeof(UInt16)) { basic.Emit(OpCodes.Stelem_I2); } else if (type == typeof(Int32)) { basic.Emit(OpCodes.Stelem_I4); } else if (type == typeof(UInt32)) { basic.Emit(OpCodes.Stelem_I4); } else if (type == typeof(Int64)) { basic.Emit(OpCodes.Stelem_I8); } else if (type == typeof(UInt64)) { basic.Emit(OpCodes.Stelem_I8); } else if (type == typeof(Single)) { basic.Emit(OpCodes.Stelem_R4); } else if (type == typeof(Double)) { basic.Emit(OpCodes.Stelem_R8); } else if (type == typeof(Decimal)) { basic.Emit(OpCodes.Stelem); } else { throw new Exception("not exist datatype!"); } }
private static LocalBuilder NewField <T>(EmitBasic basic, T value) { LocalBuilder item = basic.DeclareLocal(typeof(T)); basic.EmitValue(value); basic.Emit(OpCodes.Stloc_S, item); return(item); }
internal static FieldList <T> NewList <T>(this EmitBasic basic) { LocalBuilder item = basic.DeclareLocal(typeof(List <T>)); basic.Emit(OpCodes.Newobj, typeof(List <T>).GetConstructor(Type.EmptyTypes)); basic.Emit(OpCodes.Stloc_S, item); return(new FieldList <T>(item, basic)); }
public static LocalBuilder ArgumentRef(this EmitBasic basic, Int32 index, Type type) { LocalBuilder param = basic.DeclareLocal(type); basic.Argument(index); basic.Emit(OpCodes.Stloc_S, param); return(param); }
public static LocalBuilder ArgumentRef <T>(this EmitBasic basic, Int32 index) where T : class { LocalBuilder param = basic.DeclareLocal(typeof(T)); basic.Argument(index); basic.Emit(OpCodes.Stloc_S, param); return(param); }
internal static FieldArray <T> NewArray <T>(this EmitBasic basic, Int32 length = default(Int32)) { LocalBuilder item = basic.DeclareLocal(typeof(T[])); basic.IntegerMap(length); basic.Emit(OpCodes.Newarr, typeof(T)); basic.Emit(OpCodes.Stloc_S, item); return(new FieldArray <T>(item, basic, length)); }
internal static FieldArray <T> NewArray <T>(this EmitBasic basic, LocalBuilder length) { LocalBuilder item = basic.DeclareLocal(typeof(T[])); basic.Emit(OpCodes.Ldloc_S, length); basic.Emit(OpCodes.Newarr, typeof(T)); basic.Emit(OpCodes.Stloc_S, item); return(new FieldArray <T>(item, basic, -1)); }
public static void Throw <T>(this EmitBasic basic, String message = null) where T : Exception { var _ex = basic.DeclareLocal(typeof(T)); basic.EmitValue(message); basic.Emit(OpCodes.Newobj, typeof(T).GetConstructor(new[] { typeof(String) })); basic.Emit(OpCodes.Stloc_S, _ex); basic.Throw(_ex); }
public static void Throw(this EmitBasic basic, LocalBuilder ex) { if (ex == null) { basic.Emit(OpCodes.Rethrow); return; } basic.Emit(OpCodes.Ldloc_S, ex); basic.Emit(OpCodes.Throw); }
internal static FieldBoolean IsNull(this EmitBasic basic, LocalBuilder value) { LocalBuilder assert = basic.DeclareLocal(typeof(Boolean)); basic.Emit(OpCodes.Ldloc_S, value); basic.Emit(OpCodes.Ldnull); basic.Emit(OpCodes.Ceq); basic.Emit(OpCodes.Stloc_S, assert); return(new FieldBoolean(assert, basic)); }
public static MethodManager ReflectStaticMethod(this EmitBasic basic, String MethodName, Type type) { MethodInfo method = type.GetMethod(MethodName, Type.EmptyTypes); basic.Emit(OpCodes.Call, method); if (method.ReturnType != null && method.ReturnType != typeof(void)) { CacheManager.retValue = true; } return(new MethodManager(basic, method.ReturnType)); }
public static MethodManager ReflectStaticMethod(this EmitBasic basic, String MethodName, Type type, params LocalBuilder[] parameters) { MethodInfo method = type.GetMethod(MethodName, parameters.Select(x => x.LocalType).ToArray()); parameters.ToList().ForEach(x => basic.Emit(OpCodes.Ldloc_S, x)); basic.Emit(OpCodes.Call, method); if (method.ReturnType != null && method.ReturnType != typeof(void)) { CacheManager.retValue = true; } return(new MethodManager(basic, method.ReturnType)); }
public static void Argument(this EmitBasic basic, Int32 index) { switch (index) { case 0: basic.Emit(OpCodes.Ldarg_0); break; case 1: basic.Emit(OpCodes.Ldarg_1); break; case 2: basic.Emit(OpCodes.Ldarg_2); break; case 3: basic.Emit(OpCodes.Ldarg_3); break; default: basic.Emit(OpCodes.Ldarg_S, index); break; } }
internal static void IntegerMap(this EmitBasic basic, Int64 value) { switch (value) { case -1: basic.Emit(OpCodes.Ldc_I4_M1); break; case 0: basic.Emit(OpCodes.Ldc_I4_0); break; case 1: basic.Emit(OpCodes.Ldc_I4_1); break; case 2: basic.Emit(OpCodes.Ldc_I4_2); break; case 3: basic.Emit(OpCodes.Ldc_I4_3); break; case 4: basic.Emit(OpCodes.Ldc_I4_4); break; case 5: basic.Emit(OpCodes.Ldc_I4_5); break; case 6: basic.Emit(OpCodes.Ldc_I4_6); break; case 7: basic.Emit(OpCodes.Ldc_I4_7); break; case 8: basic.Emit(OpCodes.Ldc_I4_8); break; default: if (value < Int64.MinValue || value > Int64.MaxValue) { ShowEx("IntegerMap 数值溢出"); } else if (value < Int32.MinValue || value > Int32.MaxValue) { basic.Emit(OpCodes.Ldc_I8, value); } else if (value < SByte.MinValue || value > SByte.MaxValue) { basic.Emit(OpCodes.Ldc_I4, value); } else { basic.Emit(OpCodes.Ldc_I4_S, value); } break; } }
internal static void Forr(this EmitBasic basic, Int32 init, Int32 length, Action <CanCompute <Int32>, TabManager> build) { Label _for = basic.DefineLabel(); Label _endfor = basic.DefineLabel(); Label _break = basic.DefineLabel(); LocalBuilder index = basic.DeclareLocal(typeof(Int32)); basic.IntegerMap(init); basic.Emit(OpCodes.Stloc_S, index); basic.Emit(OpCodes.Br, _endfor); basic.MarkLabel(_for); build?.Invoke(new CanCompute <Int32>(index, basic), new TabManager(basic, _break)); basic.Emit(OpCodes.Ldloc_S, index); basic.Emit(OpCodes.Ldc_I4_1); basic.Emit(OpCodes.Sub); basic.Emit(OpCodes.Stloc_S, index); basic.MarkLabel(_endfor); basic.Emit(OpCodes.Ldloc_S, index); basic.IntegerMap(length); basic.Emit(OpCodes.Bge, _for); basic.MarkLabel(_break); }
internal static void EmitValue(this EmitBasic basic, Object value, Type type) { if (type == typeof(String)) { basic.Emit(OpCodes.Ldstr, Convert.ToString(value)); } else if (type == typeof(Boolean)) { switch (Convert.ToBoolean(value)) { case true: basic.Emit(OpCodes.Ldc_I4_1); break; case false: basic.Emit(OpCodes.Ldc_I4_0); break; default: throw new Exception("boolean to error!"); } } else if (type == typeof(SByte)) { basic.IntegerMap(Convert.ToSByte(value)); } else if (type == typeof(Byte)) { basic.IntegerMap((SByte)Convert.ToByte(value)); } else if (type == typeof(Int16)) { basic.IntegerMap(Convert.ToInt16(value)); } else if (type == typeof(UInt16)) { basic.IntegerMap((Int16)Convert.ToUInt16(value)); } else if (type == typeof(Int32)) { basic.IntegerMap(Convert.ToInt32(value)); } else if (type == typeof(UInt32)) { basic.IntegerMap((Int32)Convert.ToUInt32(value)); } else if (type == typeof(Int64)) { basic.IntegerMap(Convert.ToInt64(value)); } else if (type == typeof(UInt64)) { basic.IntegerMap((Int64)Convert.ToUInt64(value)); } else if (type == typeof(Single)) { basic.Emit(OpCodes.Ldc_R4, Convert.ToSingle(value)); } else if (type == typeof(Double)) { basic.Emit(OpCodes.Ldc_R8, Convert.ToDouble(value)); } else if (type == typeof(Decimal)) { Int32[] bits = Decimal.GetBits(Convert.ToDecimal(value)); basic.IntegerMap(bits[0]); basic.IntegerMap(bits[1]); basic.IntegerMap(bits[2]); basic.Emit((bits[3] & 0x80000000) != 0 ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); basic.IntegerMap((bits[3] >> 16) & 0x7f); basic.Emit(OpCodes.Newobj, typeof(Decimal) .GetConstructor(new Type[] { typeof(Int32), typeof(Int32), typeof(Int32), typeof(Boolean), typeof(Byte) })); } else if (type == typeof(DateTime)) { basic.Emit(OpCodes.Ldc_I8, Convert.ToDateTime(value).Ticks); basic.Emit(OpCodes.Newobj, typeof(DateTime).GetConstructor(new Type[] { typeof(Int64) })); } else { throw new Exception("not exist datatype!"); } }
internal static FieldString NewString(this EmitBasic basic, String value = default(String)) { return(new FieldString(NewField(basic, value), basic)); }
internal static FieldBoolean NewBoolean(this EmitBasic basic, Boolean value = default(Boolean)) { return(new FieldBoolean(NewField(basic, value), basic)); }
internal static CanCompute <Byte> NewByte(this EmitBasic basic, Byte value = default(Byte)) { return(new CanCompute <Byte>(NewField(basic, value), basic)); }
internal static FieldEntity <T> NewEntity <T>(this EmitBasic basic, T value) { return(new FieldEntity <T>(basic.MapToEntity(value), basic)); }
internal static FieldObject NewObject(this EmitBasic basic, Object value = default(Object)) { return(new FieldObject(NewField(basic, value), basic)); }
internal static FieldDateTime NewDateTime(this EmitBasic basic, DateTime value = default(DateTime)) { return(new FieldDateTime(NewField(basic, value), basic)); }
internal static CanCompute <Decimal> NewDecimal(this EmitBasic basic, Decimal value = default(Decimal)) { return(new CanCompute <Decimal>(NewField(basic, value), basic)); }
internal static CanCompute <Double> NewDouble(this EmitBasic basic, Double value = default(Double)) { return(new CanCompute <Double>(NewField(basic, value), basic)); }
internal static CanCompute <Single> NewFloat(this EmitBasic basic, Single value = default(Single)) { return(new CanCompute <Single>(NewField(basic, value), basic)); }
internal static CanCompute <Int64> NewInt64(this EmitBasic basic, Int64 value = default(Int64)) { return(new CanCompute <Int64>(NewField(basic, value), basic)); }