Can I use Roslyn for compile time code rewriting?
Asked Answered
T

2

16

For example I have

class Foo: INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    public int Bar {get;set;}
}

Can I get the Foo class AST and rewrite Bar, in compile time, to

    public string Bar
    {
        get { return this.bar; }

        set 
        {
            if (value != this.bar)
            {
                this.phoneNumberValue = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Bar"));
            }
        }
    }

.

Theorist answered 20/10, 2011 at 9:38 Comment(2)
FYI you can do this today with this github.com/SimonCropp/FodyRubious
The location of that code was moved. It's now at github.com/Fody/FodyLafleur
R
16

Compile time re-writing isn't directly supported by Roslyn today, but syntactic and semantic transformations definitely are. In fact, take a look at the "ImplementNotifyPropertyChanged" sample included in the CTP to see something of what you want to do. The sample is implemented as a design time transformation in and IDE feature, but you can extract the logic and make it into something like a pre-build task that rewrites files before compilation.

Randeerandel answered 20/10, 2011 at 16:54 Comment(11)
Ivan Towlson also blogged about automatic DependencyProperty implementation - mindscapehq.com/blog/index.php/2011/10/20/in-bed-with-roslynNightrider
now in 2014 so 2,5 years later: Is this answer still correct?Roldan
Yes. We are deliberately NOT building compilation hooks yet, as we'd like to see what sort of things the community builds and what scenarios emerge first so that we can build the right hooks if/when we do.Randeerandel
A humble question might be how you would observe that the community builds when you box the "compiler as a service" as a atomic hidden step in your own tools? Now we can only use it for simple tooling add-ons. What good is it to me when I'm left with the same build tasks as I've always had? We need access to the SematicModel and the SyntaxTree when we employ build taks.Aeonian
Java has had annotations that allows you to hook into the compile process and do compile time code generation for years. Somehow Microsoft seems to wan't to protect us from ourselves in a way that makes some of us feel like you are treating us like children. The annotations in Java (and the generate/recompile cycle they provide) has certainly not led to havoc or mayhem and is a very robust system.Aeonian
@KevinPilch-Bisson Another year and a few OSS releases later, is AOP in Roslyn possible?Earthbound
And now, it is possible?Pleasure
Another year. And now?Fibril
Can't resist...and now?Holston
Yay, the asking time! And now?Hornsby
Does this mean this is now possible? github.com/AArnott/CodeGeneration.RoslynHollerman
S
2

I don't think this is possible in the current CTP that has been released as the compiler is there as service but there is no such thing which allows you to plug into the compilation process as you can do in Nemerle.

Signore answered 20/10, 2011 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.