增加Object.Between方法和测试
This commit is contained in:
parent
a7617c35e1
commit
a0a4f98f61
|
@ -137,7 +137,7 @@ namespace Falcon.SugarApi.Test
|
|||
|
||||
var o1 = new object();
|
||||
Assert.IsTrue(o1.In(o1));
|
||||
Assert.IsFalse(o1.In( new object(),new object()));
|
||||
Assert.IsFalse(o1.In(new object(),new object()));
|
||||
|
||||
var o2 = new ObjWithComparable { val = 1 };
|
||||
var o22 = new ObjWithComparable { val = 1 };
|
||||
|
@ -145,6 +145,18 @@ namespace Falcon.SugarApi.Test
|
|||
Assert.IsTrue(o2.In(o22,o23));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 应用动态比较的方式比较对象是否在另外两个对象区间。
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void ObjectBetweenTest() {
|
||||
Assert.IsTrue(1.Between(0,2));
|
||||
Assert.IsFalse(3.Between(0,1));
|
||||
var now = DateTime.Now;
|
||||
Assert.IsTrue(now.Between(now.AddSeconds(-1),now.AddSeconds(1)));
|
||||
Assert.IsFalse(now.Between(now.AddSeconds(1),now.AddSeconds(2)));
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 扩展属性测试类
|
||||
|
|
|
@ -145,9 +145,32 @@ namespace Falcon.SugarApi
|
|||
/// <param name="start">开始时间</param>
|
||||
/// <param name="end">结束时间</param>
|
||||
/// <returns>True是,False否</returns>
|
||||
public static bool Between(this DateTime? dt,DateTime start,DateTime? end = null) {
|
||||
end ??= DateTime.Now;
|
||||
return dt > start && dt <= end;
|
||||
//public static bool Between(this DateTime? dt,DateTime start,DateTime? end = null) {
|
||||
// end ??= DateTime.Now;
|
||||
// return dt > start && dt <= end;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 时间是否处于某个时间段内
|
||||
/// </summary>
|
||||
/// <param name="dt">要判断的时间</param>
|
||||
/// <param name="start">开始时间</param>
|
||||
/// <param name="end">结束时间</param>
|
||||
/// <returns>True是,False否</returns>
|
||||
public static bool Between(this DateTime? dt,DateTime? start,DateTime? end) {
|
||||
if(dt==null) {
|
||||
return false;
|
||||
}
|
||||
if(start == null && end == null) {
|
||||
throw new Exception("给定的时间范围无效");
|
||||
}
|
||||
if(start == null) {
|
||||
return dt < end;
|
||||
}
|
||||
if(end == null) {
|
||||
return dt >= start;
|
||||
}
|
||||
return dt >= start && dt <= end;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -248,6 +248,37 @@ namespace Falcon.SugarApi
|
|||
/// <param name="values">目标对象数组</param>
|
||||
/// <returns>不在数组中返回True,否则False</returns>
|
||||
public static bool NotIn<T>(this T obj,params T[] values) => !obj.In(values);
|
||||
|
||||
/// <summary>
|
||||
/// 比较值是否在给定的上下限之间。包含下限但不包含上限。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">值类型</typeparam>
|
||||
/// <param name="obj">比较对象</param>
|
||||
/// <param name="min">最小值</param>
|
||||
/// <param name="max">最大值</param>
|
||||
/// <returns>是否包含在最小值和最大值之间</returns>
|
||||
/// <exception cref="Exception">给定的参数无法进行比较</exception>
|
||||
public static bool Between<T>(this T? obj,T? min,T? max) {
|
||||
if(obj == null) {
|
||||
return false;
|
||||
}
|
||||
if(min == null && max == null) {
|
||||
throw new Exception("min和max不能都为null");
|
||||
}
|
||||
dynamic? c = obj, i = min, x = max;
|
||||
try {
|
||||
if(i == null) {
|
||||
return c < x;
|
||||
}
|
||||
if(x == null) {
|
||||
return c >= i;
|
||||
}
|
||||
return c >= i && c <= x;
|
||||
}
|
||||
catch(Exception ex) {
|
||||
throw new Exception("给定的参数无法进行比较",ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Reference in New Issue
Block a user