// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using Castle.DynamicProxy; namespace Dynamic { // aspect public class Check : IInterceptor { // advice and pointcut defined in interceptor setup public void Intercept(IInvocation inv) { if(inv.Method.Name == "Divide") { object o = inv.GetArgumentValue(1); if (o is int) { if ((int)o == 0) { Console.WriteLine("AOP> About to divide by integer zero"); } } else if (o is double) { if ((double)o == 0) { Console.WriteLine("AOP> About to divide by double zero"); } } else { throw new Exception("Unsupported data type in divide: " + o.GetType().FullName); } } inv.Proceed(); } } }