Expression Tree 'Any' on Collection

.net asp.net-mvc c# expression-trees linq

Question

I'm trying to determine if any of the objects in the collection have the target property set to true. How can I accomplish this? Here's what I have so far:

public class AnyValidAttribute : ValidationAttribute
{
    private static MethodInfo AnyMethod =
        typeof(Enumerable)
          .GetMethods()
          .First(m => m.Name == "Any" && m.GetParameters().Length == 2));

    string booleanPropertyToCheck;
    Type entityType;

    public AnyValidAttribute(string booleanPropertyToCheck, Type entityType)
    {
        this.booleanPropertyToCheck = booleanPropertyToCheck;
        this.entityType = entityType;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var collection = new List<object>((IEnumerable<object>)value);
        var propInfo = this.entityType.GetProperty(this.booleanPropertyToCheck);
        var param = Expression.Parameter(this.entityType, "entity");
        var property = Expression.MakeMemberAccess(param, propInfo);
        var trueValue = Expression.Constant(true);
        var comparison = Expression.Equal(property, trueValue);
        var lambda = Expression.Lambda(comparison, param);
        var call = Expression.Call(AnyMethod, comparison, lambda);
        var anyTrue = ???

        return true ? null : new ValidationResult("At least one is required.");
    }
}
1
2
11/28/2017 7:00:45 AM

Accepted Answer

You want to call dynamically Any method?

I would suggest to do somehting like this:

var anyDelegate = lambda.Compile();
var genericMethod = AnyMethod.MakeGenericMethod(entityType);
var anyTrue = (bool)genericMethod.Invoke(null, new[] {value, genericMethod});

You don't really need your Call Expression.

Complete call inside of your method would be like (works for me in Linqpad):

var propInfo = this.entityType.GetProperty(this.booleanPropertyToCheck);

var param = Expression.Parameter(this.entityType, "entity");
var property = Expression.MakeMemberAccess(param, propInfo);
var trueValue = Expression.Constant(true);
var comparison = Expression.Equal(property, trueValue);
var lambda = Expression.Lambda(comparison, param);  

var anyDelegate = lambda.Compile();

var genericMethod = AnyMethod.MakeGenericMethod(entityType);
var anyTrue = (bool)genericMethod.Invoke(null, new[] { value, anyDelegate });
2
11/28/2017 7:00:31 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow