Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Roslyn for compile time code rewriting?

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"));
            }
        }
    }

.

like image 656
dotneter Avatar asked Oct 20 '11 09:10

dotneter


2 Answers

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.

like image 85
Kevin Pilch Avatar answered Oct 11 '22 15:10

Kevin Pilch


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.

like image 28
Ankur Avatar answered Oct 11 '22 15:10

Ankur