C#: How to create an attribute on a method triggering an event when it is invoked?

Viewed 46263

Is there a way in C# or .NET in general to create an attribute on a method which triggers an event when the method is invoked? Ideally, I would be able to run custom actions before and after the invocation of the method.

I mean something like this:

[TriggersMyCustomAction()]
public void DoSomeStuff()
{
}

I am totally clueless how to do it or if it possible at all, but System.Diagnostic.ConditionalAttribute might do a similar thing in the background. I am not sure though.

EDIT: I forgot to mention that due to the circumstances of my specific case, performance is not really an issue.

7 Answers

The only way I know how to do this is with PostSharp. It post-processes your IL and can do things like what you asked for.

You need some sort of Aspect oriented framework. PostSharp will do it, as will Windsor.

Basically, they subclass your object and override this method...

then it becomes:

//proxy
public override void DoSomeStuff()
{
     if(MethodHasTriggerAttribute)
        Trigger();

     _innerClass.DoSomeStuff();
}

of course all this is hidden to you. All you have to do is ask Windsor for the type, and it will do the proxying for you. The attribute becomes a (custom) facility I think in Windsor.

I don't think there is a way to do it with just an attribute, but using proxy classes and reflection you could have a class that knows to intercept instantiations of the classes in which you have attributed methods.

Then the proxy class can trigger an event whenever the attributed methods are called.

An attribute gives information, they are metadata. I don't know of a way to do this offhand, someone might.

You could look at partial methods in .NET which allow you to do some lightweight event handling. You provide the hooks and let someone else handle the implementation. If the method isn't implemented the compiler just ignores it.

http://msdn.microsoft.com/en-us/library/wa80x488.aspx

You might take a look at the poor man's solution: see the decorator pattern.

Related