//各种数据给我就能入栈 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 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 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); }
//设置公有字段 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); } }
public static Action CreateCompareAction(this ILGenerator il, IOperator source, object dest, OpCode code) { return(() => { source.RunCompareAction(); ThreadCache.SetJudgeCode(code); if (dest is IOperator) { ((IOperator)dest).RunCompareAction(); } else { il.NoErrorLoad(dest); } }); }
public static Action CreateOperatorAction(this ILGenerator il, IOperator source, object dest, Action action) { return(() => { source.RunCompareAction(); if (dest is IOperator) { ((IOperator)dest).RunCompareAction(); } else { il.NoErrorLoad(dest); } action(); }); }
public static void SetPublicProperty(this ILGenerator il, Action thisInStack, PropertyInfo info, object value) { MethodInfo method = info.GetSetMethod(true); //静态属性 if (!method.IsStatic) { if (thisInStack != null) { thisInStack(); } } il.NoErrorLoad(value, info.PropertyType); MethodHelper.CallMethod(method); }