突然想到写一个用于做断言的类。断言,可以在Debug过程中,不符合条件时给出错误提示。断言是在Debug时把错误提交出来,防止在Release中出现一些异常情况的工具。
虽然有断言,但是有时候一些环境是现实中比较难出现的,比如,你的事件要在2007-12-19触发,问题是现在并不是那个时间,你也不可能慢慢等,而且,你又不希望改动时间(比如你的机器在做一个服务,时间正确非常重要),可以使用模拟环境用的替身对象来实现测试这个事件触发后会不会有异常。
1
public static T GetMockValue<T> ( T DebugValue, T RealValue )
2
{
3
if ( System.Diagnostics.Debugger.IsAttached )
4
{
5
return DebugValue;
6
}
7
8
return RealValue;
9
}
可以在调试的时候返回不同的值。而Release时又返回真正的值。
2

3

4

5

6

7

8

9

只是一个参考。
------------------
广东.NET 群 3748264