예제 #1
0
        private void ImplementSetBody(PropertyDefinition property, FieldDefinition attributeField, FieldDefinition propertyInfoField, MethodDefinition method, ILProcessor il, MethodReference proceed)
        {
            // We want to call the interceptor's setter method:
            // void SetPropertyValue(PropertyInfo propertyInfo, object instance, object oldValue, object newValue, Action<object> setter)

            // Get interceptor attribute
            il.LoadField(attributeField);

            // Leave PropertyInfo on the stack as the first argument
            il.LoadField(propertyInfoField);

            // Leave the instance on the stack as the second argument
            EmitInstanceArgument(il, method);

            // Get the current value of the property as the third argument
            il.EmitThisIfRequired(property.GetMethod);
            il.EmitCall(property.GetMethod);
            il.EmitBoxIfNeeded(method.Parameters[0].ParameterType);

            // Leave the new value on the stack as the fourth argument
            il.EmitArgument(method, 0);
            il.EmitBoxIfNeeded(method.Parameters[0].ParameterType);

            // Leave the delegate for the proceed implementation on the stack as fifth argument
            il.EmitLocalMethodDelegate(proceed, Context.Action1Type, TypeSystem.ObjectReference);

            // Finally, we emit the call to the interceptor
            il.Emit(OpCodes.Callvirt, setPropertyValue);

            // Return
            il.Emit(OpCodes.Ret);
        }