using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace Tnb.Common.Utils { public class PropertyNotNullChecker { private static readonly Dictionary> Cache = new Dictionary>(); public static bool CheckAllPropertiesNotNull(T obj) { //Type type = typeof(T); //// 检查缓存中是否存在类型的检查方法 //if (Cache.TryGetValue(type, out var checkFunc)) //{ // return checkFunc(obj); //} //var properties = type.GetProperties(); //// 创建表达式树的参数 //var objParam = Expression.Parameter(type, "obj"); //var propertyNotNullExpressions = new List(); //foreach (var property in properties) //{ // // 创建访问属性的表达式 // var propertyAccess = Expression.Property(objParam, property); // // 创建检查属性非空的表达式 // var notNullExpression = Expression.NotEqual(propertyAccess, Expression.Constant(null)); // propertyNotNullExpressions.Add(notNullExpression); //} //// 创建所有属性非空的表达式 //var allPropertiesNotNullExpression = Expression.AndAlso(propertyNotNullExpressions); //// 创建Lambda表达式 //var lambda = Expression.Lambda>(allPropertiesNotNullExpression, objParam); //// 编译表达式并执行 //var func = lambda.Compile(); //// 将类型和检查方法添加到缓存中 //Cache[type] = func; //return func(obj); return false; } } }