Executing Attribute

Jun 19, 2013 at 9:44 PM
I am not sure if this is the correct place to post this but I have a feature request for the BCL. It would be great if there was a way to create a custom Attribute class that could execute code before and/or after a method execution.

This would allow for basic AOP like programming without the need for a framework like PostSharp etc.

I can imagine it being a different base class to inherit from other than Attribute that would have OnExecuting and OnExecuted methods that you could override to inject your code.

The possibilities of what could be done with this limitless but just think of a LoggingAttribute class as one example. Auditing your code would become declarative and simple, without the need for any additional frameworks.

Just my $0.02.

Thanks!
Ian
Jun 20, 2013 at 3:52 PM
Edited Jun 20, 2013 at 3:54 PM
Hi Ian,

That already exists in the BCL, though participating types must derive from ContextBoundObject. Note that it's not supported on every platform.

See ContextAttribute.

- Dave
Jun 20, 2013 at 4:02 PM
Edited Jun 20, 2013 at 4:02 PM
Hi Ian,

For example, you can derive from ContextAttribute and implement IContributeServerContextSink.

- Dave