I had a very interesting experience with AOP in C #. I have a function with a return type of List that is intercepted and that everything is fine and good. However, the interceptor function is a validator style function and can prevent the real function by calling it and returning a boolean false.
So, the code looks something like this:
List<Update> updates = Manager.ValidateAndCreate(); // protected void Save(List<Update> updates) { .... Save(updates);
The method hook looks like this
public class ExceptionAdvice : AopAlliance.Intercept.IMethodInterceptor { public object Invoke(AopAlliance.Intercept.IMethodInvocation invocation) { if (isValid(invocation)) { return invocation.Proceed(); } else { return false; } } private bool isValid( ... }
Now, after the check has failed, the value of the updates is actually a logical, not a list, I thought that there would be some kind of runtime error, but it wasn’t, so:
updates.GetType().Name == "Boolean"
But:
updates is bool == false
That way, the save will still accept its mutated list of updates and will later complain when you try to use it.
So how is this possible in a safe language like C #? btw it spring -aop.
Edit: It also compiles and it works, I’ve stepped through it several times already.
source share