如何静态验证某些 C# 代码是否有效?
在 C# 中以函数式风格编写时,是否有任何工具可以静态验证类是不可变的并且函数是纯的?我认为这在一般情况下是不可能的,但 90% 的时间都正确的工具仍然有用。
我可以想象一些反射,检查所有成员变量是否为只读,并且所有成员类型(以及所有可见子类型)也是不可变的(递归地)。我不知道功能检查将如何开始运行。
When writing in a functional style in C#, are there any tools to statically verify that classes are immutable and functions are pure? I imagine it to be impossible in the general case, but a tool that's right 90% of the time would still be useful.
I can imagine some reflection that checks that all member variables are readonly
, and that all member types (and all visible subtypes) are also immutable (recursively). I've no idea how the check on functions would begin to operate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[Immutable] 和 [Pure] 注释似乎就是针对这一点的。查看适用于 .NET 4.0 的合同设计工具。 IIRC,但静态验证仅适用于 VS Ultimate。
[Immutable] and [Pure] annotations seem to be geared towards this. Take a look at Design by Contract tools for .NET 4.0. IIRC, static verification only works with VS Ultimate, though.