我是“尽早失败”策略的粉丝,并且想检查方法参数是否具有正确的值。在 Java 中,我会使用Guava 之类的东西:
checkArgument(count > 0, "must be positive: %s", count);
.NET 有类似的东西吗?
我是“尽早失败”策略的粉丝,并且想检查方法参数是否具有正确的值。在 Java 中,我会使用Guava 之类的东西:
checkArgument(count > 0, "must be positive: %s", count);
.NET 有类似的东西吗?
代码合同:http: //msdn.microsoft.com/en-us/devlabs/dd491992
代码协定仍然是标准 Visual Studio 安装的附加组件/不是一部分,但它们确实允许您表达前置条件和后置条件以及对象不变量。
不同的选项可用于将合同作为编译时或运行时检查(或两者)来执行。
Take a look at CuttingEdge.Conditions. It allows you to write your preconditions in a fluent manner, as follows:
ICollection GetData(int? id, string xml, IEnumerable<int> col)
{
Condition.Requires(id, "id")
.IsNotNull()
.IsInRange(1, 999)
.IsNotEqualTo(128);
Condition.Requires(xml, "xml")
.StartsWith("<data>")
.EndsWith("</data>")
.Evaluate(xml.Contains("abc") || xml.Contains("cba"));
Condition.Requires(col, "col")
.IsNotNull()
.IsNotEmpty()
.Evaluate(c => c.Contains(id.Value) || c.Contains(0));
}
You need C# 3.0 or VB.NET 9.0 with .NET 2.0 or up for CuttingEdge.Conditions.