// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using Vajhoej.StaticWeaver; namespace MyStuff { // aspect [Aspect] public class Check { // pointcut: // any return type, any class MyStuff.*, Divide method, any arguments [Pointcut(Signature="* MyStuff.*::Divide(*)")] public static void MathCheck() { } // advice: // before [BeforeCallAdvice(Pointcut="MathCheck")] public static void Enter(MethodJoinpoint mjp) { object o = mjp.Arguments[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); } } } }