9

I have been looking through the walkthroughs and documentation for Roslyn and there are a lot of examples for things such as IDE-warnings/errors and refactorings, but there appears to be no mention of whether Roslyn can be used as a compilation hook (like byteweaving libraries tend to do, but before we have the generated IL).

For example, I would like to convert:

public void DoSomething(Entity e) {
    e.DoSomethingElse();
}

to:

public void DoSomething(Entity e) {
    using (e.Acquire()) {
        e.DoSomethingElse();
    }
}

... But every time the 'before' code is compiled, not as a one-time refactoring.

The meaning of my example is not important, I just wonder if such a thing is possible using Roslyn, and if any examples of such exist?

svick
  • 236,525
  • 50
  • 385
  • 514
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
  • so you would like to create something like a "precompiler"? – Florian May 20 '14 at 11:14
  • @thefiloe Yes, I guess that's what you could call it. Existing AOP libraries use post-compilation (i.e. PostSharp, http://www.postsharp.net/) as far as I know; but it would be neat, I think, to simply add the pertinent code before we pass it to csc.exe. – Xenoprimate May 20 '14 at 11:17
  • [Castle Dynamic Proxy](http://www.castleproject.org/projects/dynamicproxy) can do it at runtime. – Ufuk Hacıoğulları May 20 '14 at 12:20
  • 2
    Technically it is of course possible, but expecting full samples like that is kind of a dream, as you can check how much a license of commercial AOP product costs. – Lex Li May 20 '14 at 12:40
  • @thefiloe, AOP is kind of "post-compiler", as it usually weaves extra IL instructions into existing assemblies. – Lex Li May 20 '14 at 12:41

0 Answers0