Beispiel #1
0
        private static Getter CreateGetter(FieldInfo field)
        {
            DynamicMethod callable = CreateDynamicGetterMethod();

            Type        returnType = field.FieldType;
            ILGenerator il         = callable.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);
            EmitHelper.UnboxOrCast(il, field.DeclaringType);
            il.Emit(OpCodes.Ldfld, field);

            if (returnType.IsValueType)
            {
                il.Emit(OpCodes.Box, returnType);
            }

            il.Emit(OpCodes.Ret);

            return(callable.CreateDelegate(typeof(Getter)) as Getter);
        }
Beispiel #2
0
        private static Setter CreateSetter(FieldInfo field)
        {
            DynamicMethod callable = CreateDynamicSetterMethod();

            Type        returnType = field.FieldType;
            ILGenerator il         = callable.GetILGenerator();

            il.DeclareLocal(returnType);

            il.Emit(OpCodes.Ldarg_1);
            EmitHelper.UnboxOrCast(il, returnType);
            il.Emit(OpCodes.Stloc_0);

            il.Emit(OpCodes.Ldarg_0);
            EmitHelper.UnboxOrCast(il, field.DeclaringType);
            il.Emit(OpCodes.Ldloc_0);

            il.Emit(OpCodes.Stfld, field);
            il.Emit(OpCodes.Ret);

            return(callable.CreateDelegate(typeof(Setter)) as Setter);
        }