Exemplo n.º 1
0
        //设置私有字段
        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);
        }
Exemplo n.º 2
0
 //各种数据给我就能入栈
 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);
     }
 }
Exemplo n.º 3
0
 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);
     }
 }