C#3.0 中的契约设计
我知道C# 4.0具有Code Contract
功能,可用于实现后置条件和前置条件。但我只想使用 C# 3.0 来实现它。我正在尝试在我的工作中使用这个功能。是否可以使用attributes
来实现后置条件和前置条件?
有什么建议吗?
谢谢。
I know that C# 4.0 has the Code Contract
feature that can be used to implement post-condition and pre-condition. But I am wanting to implement it using C# 3.0 only. I am experimenting to use this feature in my work. Is it possible to use attributes
to implement post-conditions and pre-conditions?
Any advise?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将代码协定与C# 3 和.NET 3.5 结合使用。主要区别在于核心类(而不是工具)已内置到 .NET 4 中。
You can use Code Contracts with C# 3 and .NET 3.5. The main difference is that the core classes (not the tools) are built into .NET 4 already.
我仅使用 Debug.Assert 指令作为前置条件和后置条件,并手动编写一个
bool Invariant()
函数,我将其称为Debug.Assert(Invariant())
关于相关/重要公共方法的进入和退出。这是手工工作,好吧,但是非常简单,你很快就会习惯。此外,它使我能够非常密切地遵循埃菲尔的合同设计理念。
I use a mere Debug.Assert instruction for preconditions and postconditions, and I manually write a
bool Invariant()
function, which I call asDebug.Assert(Invariant())
on entry and exit of relevant/nontrivial public methods.It's manual work, ok, but it's very easy and you get used to it quickly. Also, it allows me to follow Eiffel's design by contract philosophy very closely.