| Home | Back to Index |
Some things don't quite fit the domain. (Cross-cutting concerns, plumbing.)
Free - use PostSharp.Laos (open source but business-friendly) (.Core is GPL)
Integrates with Visual Studio, MSBuild, NAnt
[Serializable] // required
public class DemoAspectAttribute // use Aspect by convention
: PostSharp.Laos.OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionEventArgs args)
{
Console.WriteLine("{0}#{1}({2})",
args.Method.DeclaringType.Name,
args.Method.Name,
args.GetReadOnlyArgumentArray().Join());
}
// Can also override OnExit() and OnException().
}
[DemoAspect] // if want to explicitly apply
public class Foo ...
// Aspect now applies to every method in class (even ctor).
// Can assign/filter at assembly level, too!
[assembly:DemoAspect(AttributeTargetTypes="Demo.Core.Aspects.*",
AttributeReplace=true, // optional filter
AttributeExclude=true, // optional filter
AttributePriority=99)]
Overuse of logging, etc. can affect performance.
Can also alter behavior; they do let you shoot yourself in the foot.
Since aspects are compiled in (IL weaving), you're "stuck" with what you put in (until recompile).
Can affect compilation speed (30%).
Reference PostSharp.Laos and PostSharp.Public.
Override CompileTimeInitialize() in aspect if want to cache things like method name, assembly name, etc.
May want to put assembly directives in an AspectMapping.cs file - can comment out to remove applying aspects.
Best to put attribute in separate namespace to make it easy to exclude. Don't want to apply the aspect to itself (compiler error).
May want to exclude Dispose methods when applying aspects.
Actually adjusts line numbers in PDB to go to right breakpoint! Can even step through aspect code in debugger.
Can do mix-ins - make class implement an interface on-the-fly and provide a concrete implementation.
Basically multiple inheritance.
Note that IntelliSense may complain if cast to interface, but it'll work.
Great for generated code.