//各种数据给我就能入栈 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); } }
internal static void EmitInt(this ILGenerator il, int value) { OpCode c; switch (value) { case -1: c = OpCodes.Ldc_I4_M1; break; case 0: c = OpCodes.Ldc_I4_0; break; case 1: c = OpCodes.Ldc_I4_1; break; case 2: c = OpCodes.Ldc_I4_2; break; case 3: c = OpCodes.Ldc_I4_3; break; case 4: c = OpCodes.Ldc_I4_4; break; case 5: c = OpCodes.Ldc_I4_5; break; case 6: c = OpCodes.Ldc_I4_6; break; case 7: c = OpCodes.Ldc_I4_7; break; case 8: c = OpCodes.Ldc_I4_8; break; default: if (value >= -128 && value <= 127) { il.REmit(OpCodes.Ldc_I4_S, (sbyte)value); } else { il.REmit(OpCodes.Ldc_I4, value); } return; } il.REmit(c); }
internal static void EmitStoreArg(this ILGenerator il, int index) { if (index <= Byte.MaxValue) { il.REmit(OpCodes.Starg_S, (byte)index); } else { il.REmit(OpCodes.Starg, index); } }
public static void UnPacket(this ILGenerator il, Type type) { if (type.IsClass && type != typeof(string) && type != typeof(object)) { il.REmit(OpCodes.Castclass, type); } else if (type.IsValueType) { il.REmit(OpCodes.Unbox_Any, type); } }
internal static void EmitLoadArgAddress(this ILGenerator il, int index) { if (index <= Byte.MaxValue) { il.REmit(OpCodes.Ldarga_S, (byte)index); } else { il.REmit(OpCodes.Ldarga, index); } }
internal static void EmitBoolean(this ILGenerator il, bool value) { if (value) { il.REmit(OpCodes.Ldc_I4_1); } else { il.REmit(OpCodes.Ldc_I4_0); } ThreadCache.SetJudgeCode(OpCodes.Brfalse_S); }
internal static void EmitLong(this ILGenerator il, long value) { if (int.MinValue <= value && value <= int.MaxValue) { il.EmitInt((int)value); } else { il.REmit(OpCodes.Ldc_I8, value); } il.REmit(OpCodes.Conv_I8); }
public static void CallMethod(MethodInfo info) { ILGenerator il = ThreadCache.GetIL(); if (info.DeclaringType.IsValueType || info.IsStatic) { il.REmit(OpCodes.Call, info); } else { il.REmit(OpCodes.Callvirt, info); } }
internal static void EmitLoadBuidlerAddress(this ILGenerator il, LocalBuilder builder) { int index = builder.LocalIndex; if (index <= Byte.MaxValue) { il.REmit(OpCodes.Ldloca_S, (byte)index); } else { il.REmit(OpCodes.Ldloca, index); } }
public static void Shl(this ILGenerator il, int dest) { if (dest < 255) { il.REmit(OpCodes.Ldc_I4_S, dest); } else { il.REmit(OpCodes.Ldc_I4, dest); } il.REmit(OpCodes.Ldc_I4_S, 31); il.REmit(OpCodes.And); il.REmit(OpCodes.Shl); }
public static void Packet(this ILGenerator il, Type type) { if (type.IsValueType) { il.REmit(OpCodes.Box, type); } }
public static void CallNullableCtor(this ILGenerator il, Type type) { if (il.IsNullable(type)) { ConstructorInfo ctor = type.GetConstructor(new Type[] { type.GenericTypeArguments[0] }); il.REmit(OpCodes.Newobj, ctor); } }
/// <summary> /// Emits a Stelem* instruction for the appropriate type. /// </summary> internal static void StoreElement(this ILGenerator il, Type type) { if (type.IsEnum) { il.REmit(OpCodes.Stelem, type); return; } switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: il.REmit(OpCodes.Stelem_I1); break; case TypeCode.Char: case TypeCode.Int16: case TypeCode.UInt16: il.REmit(OpCodes.Stelem_I2); break; case TypeCode.Int32: case TypeCode.UInt32: il.REmit(OpCodes.Stelem_I4); break; case TypeCode.Int64: case TypeCode.UInt64: il.REmit(OpCodes.Stelem_I8); break; case TypeCode.Single: il.REmit(OpCodes.Stelem_R4); break; case TypeCode.Double: il.REmit(OpCodes.Stelem_R8); break; default: if (type.IsValueType) { il.REmit(OpCodes.Stelem, type); } else { il.REmit(OpCodes.Stelem_Ref); } break; } }
//设置公有结构体字段 public static void LoadPublicField(this ILGenerator il, Action thisInStack, FieldInfo info) { if (info.IsStatic) { il.REmit(OpCodes.Ldsflda, info); } else { if (thisInStack != null) { thisInStack(); } il.REmit(OpCodes.Ldflda, info); } il.BoolInStack(info.FieldType); //il.CallNullableValue(info.FieldType); }
//设置公有字段 public static void SetPublicField(this ILGenerator il, Action thisInStack, FieldInfo info, object value) { if (info.IsStatic) { //填充静态字段 il.NoErrorLoad(value, info.FieldType); il.REmit(OpCodes.Stsfld, info); } else { if (thisInStack != null) { thisInStack(); } il.NoErrorLoad(value, info.FieldType); il.REmit(OpCodes.Stfld, info); } }
internal static void EmitNew(this ILGenerator il, ConstructorInfo ci) { if (ci.DeclaringType.ContainsGenericParameters) { return; } il.REmit(OpCodes.Newobj, ci); }
public static void SetPrivateProperty(this ILGenerator il, Action thisInStack, PropertyInfo info, object value) { //加载SetValue的第一个参数 即PropertyInfo il.REmit(OpCodes.Ldtoken, info.DeclaringType); il.REmit(OpCodes.Call, ClassCache.ClassHandle); il.REmit(OpCodes.Ldstr, info.Name); il.REmit(OpCodes.Ldc_I4_S, 44); il.REmit(OpCodes.Call, ClassCache.PropertyInfoGetter); //加载SetValue的第二个参数 if (thisInStack != null) { thisInStack(); } il.Packet(info.DeclaringType); //加载SetValue的第三个参数 il.NoErrorLoad(value, info.PropertyType); if (value != null) { il.Packet(info.PropertyType); } //调用SetValue il.REmit(OpCodes.Callvirt, ClassCache.PropertyValueSetter); }
internal static void EmitLoadArg(this ILGenerator il, int index) { switch (index) { case 0: il.REmit(OpCodes.Ldarg_0); break; case 1: il.REmit(OpCodes.Ldarg_1); break; case 2: il.REmit(OpCodes.Ldarg_2); break; case 3: il.REmit(OpCodes.Ldarg_3); break; default: if (index <= Byte.MaxValue) { il.REmit(OpCodes.Ldarg_S, (byte)index); } else { il.REmit(OpCodes.Ldarg, index); } break; } }
internal static void EmitLoadBuidler(this ILGenerator il, LocalBuilder builder) { int index = builder.LocalIndex; switch (index) { case 0: il.REmit(OpCodes.Ldloc_0); break; case 1: il.REmit(OpCodes.Ldloc_1); break; case 2: il.REmit(OpCodes.Ldloc_2); break; case 3: il.REmit(OpCodes.Ldloc_3); break; default: if (index <= Byte.MaxValue) { il.REmit(OpCodes.Ldloc_S, (byte)index); } else { il.REmit(OpCodes.Ldloc, index); } break; } }
//设置私有字段 public static void SetPrivateField(this ILGenerator il, Action thisInStack, FieldInfo info, object value) { //加载SetValue的第一个参数 即FieldInfo il.REmit(OpCodes.Ldtoken, info.DeclaringType); il.REmit(OpCodes.Call, ClassCache.ClassHandle); il.REmit(OpCodes.Ldstr, info.Name); il.REmit(OpCodes.Ldc_I4_S, 44); il.REmit(OpCodes.Callvirt, ClassCache.FieldInfoGetter); //加载SetValue的第二个参数 if (thisInStack != null) { thisInStack(); } il.Packet(info.DeclaringType); //加载SetValue的第三个参数 il.NoErrorLoad(value, info.FieldType); if (value != null) { if (il.IsNullable(info.FieldType)) { il.Packet(info.FieldType); } else { il.Packet(value.GetType()); } } //调用SetValue il.REmit(OpCodes.Callvirt, ClassCache.FieldValueSetter); }
public static void LoadPrivateProperty(this ILGenerator il, Action thisInStack, PropertyInfo info) { //加载GetValue的第一个参数 即PropertyInfo il.REmit(OpCodes.Ldtoken, info.DeclaringType); il.REmit(OpCodes.Call, ClassCache.ClassHandle); il.REmit(OpCodes.Ldstr, info.Name); il.REmit(OpCodes.Ldc_I4_S, 60); il.REmit(OpCodes.Callvirt, ClassCache.PropertyInfoGetter); //加载GetValue的第二个参数 if (thisInStack != null) { thisInStack(); } ; il.Packet(info.DeclaringType); //调用GetValue il.REmit(OpCodes.Callvirt, ClassCache.PropertyValueGetter); il.UnPacket(info.PropertyType); if (info.PropertyType.IsValueType && !info.PropertyType.IsPrimitive) { LocalBuilder builder = il.DeclareLocal(info.PropertyType); il.EmitStoreBuilder(builder); il.LoadBuilder(builder); } il.BoolInStack(info.PropertyType); //il.CallNullableValue(info.PropertyType,true); }
/// <summary> /// Emits a Stind* instruction for the appropriate type. /// </summary> internal static void EmitStoreValueIndirect(this ILGenerator il, Type type) { if (type.IsValueType) { if (type == typeof(int)) { il.REmit(OpCodes.Stind_I4); } else if (type == typeof(short)) { il.REmit(OpCodes.Stind_I2); } else if (type == typeof(long) || type == typeof(ulong)) { il.REmit(OpCodes.Stind_I8); } else if (type == typeof(char)) { il.REmit(OpCodes.Stind_I2); } else if (type == typeof(bool)) { il.REmit(OpCodes.Stind_I1); } else if (type == typeof(float)) { il.REmit(OpCodes.Stind_R4); } else if (type == typeof(double)) { il.REmit(OpCodes.Stind_R8); } else { il.REmit(OpCodes.Stobj, type); } } else { il.REmit(OpCodes.Stind_Ref); } }
//加载私有字段 public static void LoadPrivateFieldValue(this ILGenerator il, Action thisInStack, FieldInfo info) { //加载GetValue的第一个参数 即FieldInfo il.REmit(OpCodes.Ldtoken, info.DeclaringType); il.REmit(OpCodes.Call, ClassCache.ClassHandle); il.REmit(OpCodes.Ldstr, info.Name); il.REmit(OpCodes.Ldc_I4_S, 44); il.REmit(OpCodes.Callvirt, ClassCache.FieldInfoGetter); //加载GetValue的第二个参数 if (thisInStack != null) { thisInStack(); } il.Packet(info.DeclaringType); //调用GetValue il.REmit(OpCodes.Callvirt, ClassCache.FieldValueGetter); //拆箱 il.UnPacket(info.FieldType); il.BoolInStack(info.FieldType); }
public static void LoadOne(this ILGenerator il, Type type) { if (type == typeof(int)) { il.REmit(OpCodes.Ldc_I4_1); } else if (type == typeof(double)) { il.REmit(OpCodes.Ldc_R8, (double)1.00); } else if (type == typeof(long) || type == typeof(ulong)) { il.REmit(OpCodes.Ldc_I4_1); il.REmit(OpCodes.Conv_I8); } else if (type == typeof(float)) { il.REmit(OpCodes.Ldc_R4, (float)1.0); } else { il.REmit(OpCodes.Ldc_I4_1); } }
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); } }
internal static void EmitSingle(this ILGenerator il, float value) { il.REmit(OpCodes.Ldc_R4, value); }
internal static void EmitDouble(this ILGenerator il, double value) { il.REmit(OpCodes.Ldc_R8, value); }
internal static void EmitULong(this ILGenerator il, ulong value) { il.REmit(OpCodes.Ldc_I8, (long)value); il.REmit(OpCodes.Conv_U8); }
internal static void EmitUInt(this ILGenerator il, uint value) { il.EmitInt((int)value); il.REmit(OpCodes.Conv_U4); }
internal static void EmitUShort(this ILGenerator il, ushort value) { il.EmitInt(value); il.REmit(OpCodes.Conv_U2); }