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

.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
dotneter
  • 1,689
  • 2
  • 15
  • 24
  • 1
    FYI you can do this today with this https://github.com/SimonCropp/Fody – Simon Jan 28 '12 at 10:48
  • 1
    The location of that code was moved. It's now at [github.com/Fody/Fody](http://github.com/Fody/Fody) – JKor Apr 30 '13 at 21:23

2 Answers2

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.

Kevin Pilch
  • 11,485
  • 1
  • 39
  • 41
  • 3
    Ivan Towlson also blogged about automatic `DependencyProperty` implementation - http://www.mindscapehq.com/blog/index.php/2011/10/20/in-bed-with-roslyn/ – Ron Warholic Oct 21 '11 at 17:33
  • now in 2014 so 2,5 years later: Is this answer still correct? – jeromerg May 20 '14 at 13:14
  • 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. – Kevin Pilch May 20 '14 at 14:39
  • 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. – Jack Wester Jul 08 '14 at 08:37
  • 2
    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. – Jack Wester Jul 08 '14 at 09:16
  • 7
    @KevinPilch-Bisson Another year and a few OSS releases later, is AOP in Roslyn possible? – Adam Houldsworth Jun 25 '15 at 10:48
  • 2
    And now, it is possible? – vernou May 19 '16 at 11:27
  • 2
    Another year. And now? – orellabac Jul 19 '17 at 05:41
  • Can't resist...and now? – lightw8 Aug 16 '18 at 21:38
  • Yay, the asking time! And now? – kkm inactive - support strike Aug 28 '18 at 21:53
  • Does this mean this is now possible? https://github.com/AArnott/CodeGeneration.Roslyn – Mike Marynowski Feb 26 '19 at 14:43
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.

Ankur
  • 33,367
  • 2
  • 46
  • 72