CodeContract.Requires(param != null) 并不能证明 param 不会为 null?
你好 我有一个方法如下:
public static PasswordCredential Create(string password, string username, string pinCode = null)
{
Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(password), "Invalid Argument: password");
Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(username), "Invalid Argument: username");
PasswordCredential credential = new PasswordCredential();
UTF8Encoding encoder = new UTF8Encoding();
SHA512Managed sha512hasher = new SHA512Managed();
credential.PasswordHash = sha512hasher.ComputeHash(encoder.GetBytes(password)); <-- Requires unproven: s != null
credential.Username = username;
credential.PinCode = pinCode;
return credential;
}
这是否意味着 Contract.Requires 不能证明表达式?如果是的话那有什么用呢? :?
更新
好吧,我发现代码合约有一个非常奇怪的行为。我将此方法移至另一个项目并且有效。不再需要未经证实的警告。然后我回到原来的项目,在消息窗口中,我找到了这一行: 消息 1 CodeContracts:建议的前提条件:Contract.Requires(password != null); 当我双击该项目时,我将导航到PasswordCredential 的PinCode 属性。
[DataMember]
public string PinCode
{
get { return _PinCode; }
set { _PinCode = value == null ? null : value.Trim(); } <-- I'm navigated here
}
但是当我单击警告项时,我将导航到encoder.GetBytes(password) 行。 我不明白出了什么问题。这是一个错误吗?
Hello
I have a method listed bellow:
public static PasswordCredential Create(string password, string username, string pinCode = null)
{
Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(password), "Invalid Argument: password");
Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(username), "Invalid Argument: username");
PasswordCredential credential = new PasswordCredential();
UTF8Encoding encoder = new UTF8Encoding();
SHA512Managed sha512hasher = new SHA512Managed();
credential.PasswordHash = sha512hasher.ComputeHash(encoder.GetBytes(password)); <-- Requires unproven: s != null
credential.Username = username;
credential.PinCode = pinCode;
return credential;
}
Does this mean that Contract.Requires doesn't prove the expression? If so what's the use for it? :?
UPDATE
Ok, I found a very weird behavior of code contracts. I moved this method to another project and it worked. No more requires unproven warnings. Then I get back to the original project, and in Messages window, I found this line:
Message 1 CodeContracts: Suggested precondition: Contract.Requires(password != null);
When I double click this item, I'm navigated to PasswordCredential's PinCode property.
[DataMember]
public string PinCode
{
get { return _PinCode; }
set { _PinCode = value == null ? null : value.Trim(); } <-- I'm navigated here
}
but when I click the warning item, I'm navigated to encoder.GetBytes(password) line.
I don't understand what's wrong. Is it a bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的
CC 有一个问题(只能在我的项目中重现:D)。我将项目发送给CC团队并得到了非常快的回复。他们正在调查,可能会在下一个版本中修复。
不管怎样,谢谢你们的支持。
Ok
There's an issue with CC (which can be reproduced only in my project :D). I sent the project to CC team and got very fast response. They are investigating and probably it will be fixed in the next release.
Anyway thanks for your support guys.