我可以使用 Roslyn 进行编译时代码重写吗?

发布于 2024-12-11 03:27:32 字数 528 浏览 0 评论 0原文

例如,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-12-18 03:27:32

如今,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.

不乱于心 2024-12-18 03:27:32

我认为这在当前已发布的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文