// Searching for AOP-enabling features in existing (java-ish) languages // Attributes: .Net description is a bit cryptic: /* " An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. ... Attributes are used for adding metadata, such as compiler instruction and other information such as comments, description, methods and classes to a program. " There are three built-in attributes: Obsolete, Conditional, and AttributeUsage which describes built-in attributes. */ //#define USEF // what if commented out? using System; using System.Diagnostics; // for Conditional Attribute public class attributes { [Conditional("USEF")] // applies only to one method immediately beneath, // return type of method must be void. public static void f() { Console.WriteLine("Hi I'm F"); } public static void g() { Console.WriteLine("Give me a G!"); } public static void Main(string[] av) { f(); g(); if (av.Length>0) f(); else g(); }//main } /* The Conditionl attribute, if '#define USEF' was commented out, erased the defintion of f as well as all calls to f (so naturally f cannot return a value). What's happening? Clearly the attribute is injecting code into your program at compile-time, since it checks for a static macro definition. So the compiled code is actually a different program. Conditional actually refers to System.Diagnostics.ConditionalAttribute class, and you can define your own attributes. Along with reflection, we can conceivably define our own attributes to inject other kinds of code into our program, and if designed correctly, our attributes can "crosscut" different classes. For example, we can consider a "trace attribute" (one may already exist) that uses reflection to automatically trace method calls. Or, a "formatting attribute" that make sure that all output conforms to some formatting protocol. But implementing your own attributes is not a fun task. There's a comerical package called "PostSharp" that's does this for you, and provides most of the AOP functionalities we desire. Postsharp also extends the C# language, but does so in a minimal way. But we will look at AOP in the form of a direct language extension: AspectJ. */