Create Dynamic Func from Object

前端 未结 2 463
忘了有多久
忘了有多久 2021-01-01 06:18

I have a criteria object in which I was to turn each property into a func, if it\'s value isn\'t null.

public class TestClassCriteria
{
    public bool? Colu         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-01 07:07

    Your current expression is comparing the property names, but I think you want to be comparing the property values:

    var funcs = new List>();
    
    foreach (var property in criteria.GetType().GetProperties())
    {
        funcs.Add(x => x.GetType().GetProperty(property.Name).GetValue(x, null) == property.GetValue(criteria, null));
    }
    

    However, are you sure you need to do this? There would probably be a better way to refactor your code so that you don't need to use reflection to compare properties that happen to have the same name on two unrelated objects.

提交回复
热议问题