我可以使用 Roslyn 进行编译时代码重写吗?
例如,
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"));
}
}
}
.
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"));
}
}
}
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如今,Roslyn 并不直接支持编译时重写,但语法和语义转换肯定是支持的。事实上,查看一下 CTP 中包含的“ImplementNotifyPropertyChanged”示例即可了解您想要执行的操作。该示例是作为 IDE 功能中的设计时转换实现的,但您可以提取逻辑并将其制作成类似预构建任务的东西,在编译之前重写文件。
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.
我认为这在当前已发布的 CTP 中是不可能的,因为编译器是作为服务存在的,但没有这样的东西允许您像 Nemerle 中那样插入编译过程。
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.