PDA

View Full Version : dynamically setting Interception Around Advice for just one call



sideout
10-13-2008, 03:14 PM
public class DebugInterceptor : IMethodInterceptor {

public object Invoke(IMethodInvocation invocation) {
Console.WriteLine("Before: invocation=[{0}]", invocation);
object rval = invocation.Proceed();
Console.WriteLine("Invocation returned");
return rval;
}
}

public class TestPage : System.Web.UI.Page
{
public void TestMethod(int a1, int a2)
{
Response.Write(String.Format("{0} {1}",a1,a2));
}

protected void Page_Load(object sender, EventArgs e)
{
// do some magic here to setup a proxy for just this call
//var testMethodProxy = ...TestMethod=>DebugInterceptor;
testMethodProxy(1,2);
}
}

Even if this could be done, it's still not ideal because DebugInterceptor does not have access to any of the state of TestPage. Ideally a method of TestPage or a delegate could be used as the interceptor.

The base problem I'm trying to solve is I have many different method calls in my class (with varying signatures) that I'd like to run the same specialized exception handling try/catch block on. Right now I'm repeating the wrapping try/catch code in every place and I think there must be a neater way. Some of the handling code involves calling error handling or logging methods on the page and sometimes rethrowing and sometimes not so the ExceptionAdvice is not flexible enough for me to use that.