有趣的 C# Contract.Requires bug with comments?
我有以下代码:
Contract.Requires(somecondition, "some message"/*some comment*/);
在调试时,在某个时刻未满足条件,并且它用通常的文本以及注释引发了异常! :P 所以我得到了这个例外:
ContractException was unhandled.
Precondition failed: somecondition. "some message"/*some comment*/
所以我的问题是,这是一个错误还是什么?该方法的第二个参数是字符串类型,但它只接受文字,所以我知道整个方法是一个黑客,但它不应该接受该注释,对吧?
I had the following code:
Contract.Requires(somecondition, "some message"/*some comment*/);
and while debugging at some moment the condition wasn't met, and it threw the exception with the usual text, plus the comment! :P So I got this exception:
ContractException was unhandled.
Precondition failed: somecondition. "some message"/*some comment*/
So my question is, is this a bug or what? The second parameter of the method is of type string, but it only accepts literals, so I know the entire method is a hack, but it shouldn't accept that comment, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为“代码契约重写器”重写了语句,以使用户能够看到生成的异常消息中的所有信息(包括条件表达式本身,显然还有注释)。
http://visualstudiomagazine.com/articles/2010/06/23/code -contracts.aspx
This is because the "Code Contracts rewriter" rewrites the statement to enable the user to see all the information in the resulting exception message (including the condition expression itself, and apparently comments too).
http://visualstudiomagazine.com/articles/2010/06/23/code-contracts.aspx