枚举类型增加了IsNullOrEmpty方法,判断枚举是否为null或者没有任何元素,并进行测试。

This commit is contained in:
falcon 2023-03-02 10:38:38 +08:00
parent 128f5f6025
commit f5a44f9569
2 changed files with 85 additions and 51 deletions

View File

@ -120,6 +120,19 @@ namespace Falcon.SugarApi.Test
var initVal = arr.Reduce((a,b) => throw new Exception("空集合不可以调用缩减方法"),"abc");
Assert.IsTrue(initVal=="abc","空集合返回初始值,并且不调用缩减方法。");
}
[TestMethod("IsNullOrEmpty测试")]
public void IsNullOrEmptyTest() {
List<int> list = null;
Assert.IsTrue(list.IsNullOrEmpty());
Assert.IsFalse(list.IsNotNullOrEmpty());
list=new List<int>();
Assert.IsTrue(list.IsNullOrEmpty());
Assert.IsFalse(list.IsNotNullOrEmpty());
list.Add(1);
Assert.IsFalse(list.IsNullOrEmpty());
Assert.IsTrue(list.IsNotNullOrEmpty());
}
}
internal class ToDataTableTestModel

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
@ -41,7 +42,8 @@ namespace Falcon.SugarApi
var val = p.GetValue(i);
try {
row[p.Name]=val;
} catch(Exception ex) {
}
catch(Exception ex) {
throw new Exception($"值设置失败!{p.Name}:{val}",ex);
}
}
@ -118,10 +120,29 @@ namespace Falcon.SugarApi
throw new ArgumentNullException(nameof(reduceFunc));
}
var result = initialValue;
if(source.IsNull()) {
}
foreach(var i in source) {
result=reduceFunc(result,i);
}
return result;
}
/// <summary>
/// 返回枚举是否为null或者集合无元素
/// </summary>
/// <typeparam name="T">枚举的元素类型</typeparam>
/// <param name="values">枚举对象</param>
/// <returns>为null或者无元素返回True否则False</returns>
public static bool IsNullOrEmpty<T>([AllowNull] this IEnumerable<T> values) => values==null||values.Count()==0;
/// <summary>
/// 返回枚举是否不为null或者集合无元素结果是对IsNullOrEmpty去反。
/// </summary>
/// <typeparam name="T">枚举的元素类型</typeparam>
/// <param name="values">枚举对象</param>
/// <returns>枚举不为null或者无元素返回True否则False</returns>
public static bool IsNotNullOrEmpty<T>([AllowNull] this IEnumerable<T> values) => !values.IsNullOrEmpty();
}
}