// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; namespace NewStuff { // aspect public aspect Check2 { // pointcut: // any return type, any class MyStuff.*, Divide method, any arguments pointcut MathCheck() : execution(public static any any.Divide(..)); // advice: // around object around() : MathCheck() && aroundptr(adp) { object o = thisJoinPoint.Args[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); } return adp.InnerInvoke(); } } }