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);
        }
Exemple #2
0
        private void ImplementGetBody(FieldDefinition attributeField, FieldDefinition propertyInfoField, MethodDefinition method, ILProcessor il, MethodReference proceed)
        {
            // We want to call the interceptor's setter method:
            // void GetPropertyValue(PropertyInfo propertyInfo, object instance, Action<object> getter)

            // 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);

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

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

            // Now unbox the value if necessary
            il.EmitUnboxIfNeeded(method.ReturnType, method.DeclaringType);

            // Return
            il.Emit(OpCodes.Ret);
        }
        private void ImplementBody(EventDefinition @event, FieldDefinition attributeField, FieldDefinition eventInfoField,
                                   MethodDefinition method, ILProcessor il, MethodReference proceed, MethodReference addOrRemoveHandler)
        {
            // We want to call the interceptor's AddEventHandler method:
            // void AddEventHandler(EventInfo eventInfo, object instance, Delegate handler, Action<Delegate> proceed)

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

            // Leave EventInfo on the stack as the first argument
            il.LoadField(eventInfoField);

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

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

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

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

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