I'd like to be able to parse a Predicate and see the properties that have been set and the values to which they've been set. Is this possible?
For example, given a predicate of Person, I'd like to be able to see the expressions provided, as in:
Id > 5
or
Name = "Will"
etc.
I've been toying with different things, but I don't think I'm on the right path and I can't find an example. I did find this: Convert Predicate<T> to Expression<Func<T, bool>> but the responders seemed to strongly discourage this approach. Anyone have any thoughts on this?
For the record, I'm trying to convert the predicate to an inline SQL WHERE clause.
internal class Parser
{
private IEnumerable<Person> people;
public Parser(params Person[] people)
{
this.people = new List<Person>(people);
}
public IEnumerable<Person> Parse(Predicate<Person> predicate)
{
//IFormatter formatter = new BinaryFormatter();
//IFormatterConverter converter = new FormatterConverter();
//SerializationInfo info = new(typeof(Person), converter);
//StreamingContext streamingContext = new StreamingContext();
//predicate.GetObjectData(info, streamingContext);
var invocationList = predicate.GetInvocationList();
return Enumerable.Empty<Person>();
}
}