16

例如我有

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

我可以在编译时获取 Foo 类 AST 并重写 Bar,以

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

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

.

4

2 回答 2

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.

于 2011-10-20T16:54:10.737 回答
2

我认为这在已发布的当前 CTP 中是不可能的,因为编译器作为服务存在,但没有这样的东西可以让您像在 Nemerle 中那样插入编译过程。

于 2011-10-20T10:13:38.543 回答