views:

35

answers:

1

Ideally I'd like to find a way to do something like:

[MyCustomAttribute()]
public void MyMethod()
{
    Debug.Write("B");
}

public MyCustomAttribute : Attribute
{
    public void OnBegin()
    {
        Debug.Write("A");   
    }

    public void OnEnd()
    {
        Debug.Write("C");
    }
}

Which would write:

ABC

When MyMethod() gets excuted. Any ideas if this can actually be done? The ActionFilter attribute seems similar to this idea, but it only applies to MVC requests.

+3  A: 

Take a look at PostSharp - it pretty much does this already.

It is a post processor - it takes your compiled assembly, looks for the attributes and injects code into the decorated methods.

Oded
Interesting, thanks.
John