我已经在 MSDN 的CodeContracts 论坛上发布了这个,但显然没有人知道或费心去研究这个问题。
我试图减少重复的断言并使其更可重用,但不幸的是这不起作用你能解释为什么吗?
[ContractVerification(false)]
public static class Assert
{
[Conditional("DEBUG")]
public static void GreaterThan<T>(T value, T lowerBound) where T : IComparable<T>
{
Contract.Ensures(value.CompareTo(lowerBound) > 0);
}
[Conditional("DEBUG")]
public static void GreaterThanOrEqual<T>(T value, T lowerBound) where T : IComparable<T>
{
Contract.Ensures(value.CompareTo(lowerBound) >= 0);
}
[Conditional("DEBUG")]
public static void LessThan<T>(T value, T upperBound) where T : IComparable<T>
{
Contract.Ensures(value.CompareTo(upperBound) < 0);
}
[Conditional("DEBUG")]
public static void LessThanOrEqual<T>(T value, T upperBound) where T : IComparable<T>
{
Contract.Ensures(value.CompareTo(upperBound) <= 0);
}
[Conditional("DEBUG")]
public static void NotNull(object value)
{
Contract.Ensures(value != null);
}
[Conditional("DEBUG")]
public static void NotNullOrEmpty(string value)
{
Contract.Ensures(!string.IsNullOrEmpty(value));
}
[Conditional("DEBUG")]
public static void True(bool value)
{
Contract.Ensures(value);
}
[Conditional("DEBUG")]
public static void False(bool value)
{
Contract.Ensures(!value);
}
[Conditional("DEBUG")]
public static void InRange<T>(T value, T lowerBound, T upperBound, ExclusionMode exclusionMode = ExclusionMode.None) where T : IComparable<T>
{
Contract.Ensures(((exclusionMode | ExclusionMode.LowerBound) == ExclusionMode.LowerBound ? value.CompareTo(lowerBound) > 0 : value.CompareTo(lowerBound) >= 0) && ((exclusionMode | ExclusionMode.UpperBound) == ExclusionMode.UpperBound ? value.CompareTo(upperBound) < 0 : value.CompareTo(upperBound) <= 0));
}
}
我将其更改为以下内容,它似乎可以工作,但显然通用版本更可取。
[ContractVerification(false)]
public static class Assert
{
[Conditional("DEBUG")]
public static void GreaterThan(int value, int lowerBound)
{
Contract.Ensures(value > lowerBound);
}
[Conditional("DEBUG")]
public static void GreaterThanOrEqual(int value, int lowerBound)
{
Contract.Ensures(value >= lowerBound);
}
[Conditional("DEBUG")]
public static void LessThan(int value, int upperBound)
{
Contract.Ensures(value < upperBound);
}
[Conditional("DEBUG")]
public static void LessThanOrEqual(int value, int upperBound)
{
Contract.Ensures(value <= upperBound);
}
[Conditional("DEBUG")]
public static void NotNull(object value)
{
Contract.Ensures(value != null);
}
[Conditional("DEBUG")]
public static void NotNullOrEmpty(string value)
{
Contract.Ensures(!string.IsNullOrEmpty(value));
}
[Conditional("DEBUG")]
public static void True(bool value)
{
Contract.Ensures(value);
}
[Conditional("DEBUG")]
public static void False(bool value)
{
Contract.Ensures(!value);
}
[Conditional("DEBUG")]
public static void InRange(int value, int lowerBound, int upperBound, ExclusionMode exclusionMode = ExclusionMode.None)
{
Contract.Ensures(((exclusionMode | ExclusionMode.LowerBound) == ExclusionMode.LowerBound ? value > lowerBound : value >= lowerBound) && ((exclusionMode | ExclusionMode.UpperBound) == ExclusionMode.UpperBound ? value < upperBound : value <= upperBound));
}
}
我只想要一个解释,甚至不是一个解决方案,它是否与 CodeContracts 不直接在源代码上运行但在 IL 上有关?