.Net 3.5 使用代码契约实现 String.IsNullOrWhitespace
我正在尝试在我的 .Net 3.5 (C#) 项目中使用合同。我发现我在哪里写过类似 if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(s.Trim())) throw new ArgumentException("Required", "s");
的内容方法的开始。
我可以将它们留在那里并使用遗留合同。我可以将其更改为 Contract.Requires(s!= null && string.IsNullOrEmpty(s.Trim()));
。但除了不太确定条件的空白部分的正确性或性能之外,这些都是蹩脚的(意见)。
相反,我尝试将条件封装在合同中使用的方法中。问题是我仍然不知道如何以声明方式描述 Whitespace 部分。静态检查器会根据我的编写方式发现几个问题。
我想听听有关数据内容检查是否适当的评论,例如针对空格以及正确定义的检查。
public static class str
{
[Pure]
public static bool IsNullOrWhitespace(string s)
{
Contract.Ensures(s != null || Contract.Result<bool>());
Contract.Ensures((s != null && !Contract.ForAll<char>(s, c => char.IsWhiteSpace(c))) || Contract.Result<bool>());
if (s == null) return true;
for (var i = 0; i < s.Length; i++)
{
if (!char.IsWhiteSpace(s, i))
{
Contract.Assume(!Contract.ForAll<char>(s, c => char.IsWhiteSpace(c)));
return false;
}
}
return true;
}
}
这可能有些草率,但我最初的实验方法就在这里。 a、b 和 c 上的断言失败或未经证实。
static void Test()
{
string a = null;
string b = "";
string c = " ";
string d = "xyz";
string e = " xyz ";
if (!str.IsNullOrWhitespace(a))
{
Contract.Assert(a != null);
a = a.Trim();
}
if (!str.IsNullOrWhitespace(b))
{
Contract.Assert(b != null && b != "");
b = b.Trim();
}
if (!str.IsNullOrWhitespace(c))
{
Contract.Assert(c != null && c != " ");
c = c.Trim();
}
if (!str.IsNullOrWhitespace(d))
{
d = d.Trim();
}
if (!str.IsNullOrWhitespace(e))
{
e = e.Trim();
}
}
I'm attempting to use Contracts in my .Net 3.5 (C#) project. I found where I had written something like if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(s.Trim())) throw new ArgumentException("Required", "s");
at the beginning of methods.
I could keep them there and use legacy contracts. I could change it to Contract.Requires(s!= null && string.IsNullOrEmpty(s.Trim()));
. But besides not being very sure about the correctness or performance of the Whitespace part of the condition, those are lame (opinion).
Instead, I tried to encapsulate the condition in a method for use in Contracts. Problem is that I still can't figure out how to describe the Whitespace part declaratively. The static checker finds several issues depending on how I write it.
I'd like to hear comments about the appropriateness of data content checks such as this one for whitespace AS WELL AS a correct definition.
public static class str
{
[Pure]
public static bool IsNullOrWhitespace(string s)
{
Contract.Ensures(s != null || Contract.Result<bool>());
Contract.Ensures((s != null && !Contract.ForAll<char>(s, c => char.IsWhiteSpace(c))) || Contract.Result<bool>());
if (s == null) return true;
for (var i = 0; i < s.Length; i++)
{
if (!char.IsWhiteSpace(s, i))
{
Contract.Assume(!Contract.ForAll<char>(s, c => char.IsWhiteSpace(c)));
return false;
}
}
return true;
}
}
This might be sloppy, but my initial experimentation method is here. The Asserts on a, b, and c are failing or are unproven.
static void Test()
{
string a = null;
string b = "";
string c = " ";
string d = "xyz";
string e = " xyz ";
if (!str.IsNullOrWhitespace(a))
{
Contract.Assert(a != null);
a = a.Trim();
}
if (!str.IsNullOrWhitespace(b))
{
Contract.Assert(b != null && b != "");
b = b.Trim();
}
if (!str.IsNullOrWhitespace(c))
{
Contract.Assert(c != null && c != " ");
c = c.Trim();
}
if (!str.IsNullOrWhitespace(d))
{
d = d.Trim();
}
if (!str.IsNullOrWhitespace(e))
{
e = e.Trim();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 .NET 4 的内置协定:
尽管它很复杂,但您可以看到没有提及空格。我已经尝试了几次,但我无法得到满足静态检查器的工作。
似乎有几个问题阻止我编写它,因此我在代码契约论坛上提出了一些问题。
我的合同是:
Here's the built-in contract from .NET 4:
Despite its complexity, you can see that there is no mention of whitespace. I've tried several times, but I can't get something working that will satisfy the static checker.
There appear to be a couple of issues stopping me from writing it, so I've filed some questions on the Code Contracts forum.
My contract was: