gpt4 book ai didi

c# - 从对象创建动态 Func

转载 作者:行者123 更新时间:2023-11-30 14:32:50 26 4
gpt4 key购买 nike

我有一个条件对象,如果它的值不为空,我将在其中将每个属性转换为一个函数。

public class TestClassCriteria
{
public bool? ColumnA { get; set; }
public bool? ColumnB { get; set; }
}

这是我目前所拥有的,但我很确定我没有正确定义 lambda。这就是我想要实现的目标。 funcs.Add(x => x.ColumnA == criteria.ColumnA)

var properties = criteria.GetType().GetProperties();
var funcs = new List<Func<dynamic, bool>>();

foreach (var property in properties)
{
var propertyName = property.Name;

funcs.Add(x => x.GetType().GetProperty(propertyName).Name == criteria.GetType().GetProperty(propertyName).Name);
}

它没有崩溃或导致任何错误,它只是不工作。

如果您能提供任何帮助,我们将不胜感激。

最佳答案

你想要这样的东西吗?

    static List<Func<TEntity, TCriteria, bool>> GetCriteriaFunctions<TEntity, TCriteria>()
{
var criteriaFunctions = new List<Func<TEntity, TCriteria, bool>>();

// searching for nullable properties of criteria
var criteriaProperties = typeof(TCriteria)
.GetProperties()
.Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>));

foreach (var property in criteriaProperties)
{
// this is entity parameter
var entityParameterExpression = Expression.Parameter(typeof(TEntity));
// this is criteria parameter
var criteriaParameterExpression = Expression.Parameter(typeof(TCriteria));
// this is criteria property access: "criteria.SomeProperty"
var criteriaPropertyExpression = Expression.Property(criteriaParameterExpression, property);
// this is testing for equality between criteria property and entity property;
// note, that criteria property should be converted first;
// also, this code makes assumption, that entity and criteria properties have the same names
var testingForEqualityExpression = Expression.Equal(
Expression.Convert(criteriaPropertyExpression, property.PropertyType.GetGenericArguments()[0]),
Expression.Property(entityParameterExpression, property.Name));

// criteria.SomeProperty == null ? true : ((EntityPropertyType)criteria.SomeProperty == entity.SomeProperty)
var body = Expression.Condition(
Expression.Equal(criteriaPropertyExpression, Expression.Constant(null)),
Expression.Constant(true),
testingForEqualityExpression);

// let's compile lambda to method
var criteriaFunction = Expression.Lambda<Func<TEntity, TCriteria, bool>>(body, entityParameterExpression, criteriaParameterExpression).Compile();

criteriaFunctions.Add(criteriaFunction);
}

return criteriaFunctions;
}

示例实体和示例标准:

class CustomerCriteria
{
public int? Age { get; set; }
public bool? IsNew { get; set; }
}

class Customer
{
public string Name { get; set; }
public int Age { get; set; }
public bool IsNew { get; set; }
}

用法:

        var criteriaFunctions = GetCriteriaFunctions<Customer, CustomerCriteria>();
var customer1 = new Customer { Name = "John", Age = 35, IsNew = false };
var customer2 = new Customer { Name = "Mary", Age = 27, IsNew = true };
var criteria1 = new CustomerCriteria { Age = 35 };
var criteria2 = new CustomerCriteria { IsNew = true };

Console.WriteLine("Is match: {0}", criteriaFunctions.All(f => f(customer1, criteria1)));
Console.WriteLine("Is match: {0}", criteriaFunctions.All(f => f(customer2, criteria1)));
Console.WriteLine("Is match: {0}", criteriaFunctions.All(f => f(customer1, criteria2)));
Console.WriteLine("Is match: {0}", criteriaFunctions.All(f => f(customer2, criteria2)));

此代码使用强类型成员访问,而不是您的动态代码,因此,您可以缓存每对“实体 - 条件”的条件列表,并更快地形成匹配的测试实例。

关于c# - 从对象创建动态 Func<T, TResult>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17738499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com