How to modify code before compilation?

asked10 years ago
last updated10 years ago
viewed4.8k times
Up Vote49Down Vote

Using Roslyn, I would like to modify my C# code before the actual compilation. For now, I will just need something like:

[MyAnotatedMethod]
public void MyMethod() 
{
    // method-body 
}

And based on the annotation, I would like to inject some code at the beginning of the method, and at the end of the method.

I'm aware of PostSharp, but that's not what I would like.

Is this possible to do with Roslyn? And if yes, could you give me an example?