PostSharp is a very impressive framework that makes the task of doing AOP a very pleasing process.

I’ve been so impressed that I’ve recently decided that we’ll be making use of it during development of the next generation of my company’s main product line. The very notion of IL weaving is something that terrifies some individuals (hell, it may even be Cthulhu-ian to some); however, it is definitely the kind of thing that interests me, and I’ve found the stability of using PostSharp responsibly to be overwhelmingly satisfactory.

Hopefully this story will remain the same as the code dependent on it travels towards production.

But this article isn’t about me musing about these general sorts of things, but rather me briefly musing about something very specific instead: aspect inheritance propagation.

After designing a new aspect which you refer to, rather colloquially, as MagicPower, you apply this shiny new thing to a particular base class that will be serving as the foundation for a family of classes.

    [MagicPower]
    public abstract class BaseObject<T>
    { 

    }

Perhaps the aspect is one that has a large influence on the overall behavior of the object in some way. Then maybe it is your wish that the aspect you are applying to the base will also apply to all derived classes.

It is no problem for you to decorate the base class with the aspect’s attribute, however how can you be sure that all derivations (perhaps some not made by yourself) to your class will also have this aspect?

If this aspect was just a normal attribute, you’d be pretty much out of luck — attributes are not inherited by derived classes. There’s no way to force the compiler to do this for you.

But PostSharp is special, and so is our attribute, which, besides being a normal System.Attribute, is also a MulticastAttribute. Therefore, I’m happy to say that it is very easy to achieve automatic propagation of your aspect to derived classes, and you do this by setting theAttributeInheritance property like so:

    [MagicPower(AttributeInheritance = MulticastInheritance.Multicast)]
    public abstract class BaseObject<T>
    { 

    }

That’s it.

This article was rather rudimentary in comparison to what I normally yammer on about, yes; I’ll be covering much more involved topics in regards to PostSharp in the future.

© 2012-2013 Matt Weber. All Rights Reserved. Terms of Use.