滥用 .NET 4.0 接口代码契约功能来实现 MixIns 功能
.NET 4.0 具有新的代码契约功能。它也适用于接口,如下所述(向下滚动到评论中的某处):
现在我的问题是,我们是否可以通过将更多自定义代码放入这些合约类中来使用和滥用这个“默认接口实现功能”来实现一些 MixIns 功能?
.NET 4.0 has that new Code Contracts feature. It works with interfaces too, as described here (scroll down to somewhere in the comments):
Now my question is, can we use and abuse this "Default Interface Implementation Feature" by putting more custom code into those contract classes to achieve some MixIns functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不这么认为。当为接口定义代码契约时,您可以添加一个包含“requires”和“ensures”方法调用的关系类,这些方法调用将添加到实现方法中,但代码重写器只会在编译器完成其工作后运行(即抱怨接口方法不是由类实现的...)
可能会被滥用来实现方面,尽管:-)
无论如何,我认为代码重写器不会添加 require 和 Ensure 方法之外的任何内容。并且代码不会添加到发布程序集中,只会添加到调试程序集中。
I don't think so. When defining code contracts for an interface, you can add a rela class that contains "requires" and "ensures" method calls that will be added to implementation methods, but the code rewriter will only run after the compiler has done its job (ie complaining that the interface method is not implemented by the class...)
Might be abused to implement aspects, though :-)
Anyway, I don't think the code rewriter would add anything else than requires and ensures method. And the code would not be added to the release assembly, only on the debug assembly.