Let's assume I have a method like this:
public static List<T> Get<T>(this SomeObject<T>, Expressions<Func<T,bool>> e){
//get the property name and value they want to check is true / false
...
}
TheObject().Get(x => x.PropertyName == "SomeValue");
How do I get "PropertyName" and "SomeValue" when I pass it into the Get extension method?
I think this is what you're after
BinaryExpression expression = ((BinaryExpression)e.Body);
string name = ((MemberExpression)expression.Left).Member.Name;
Expression value = expression.Right;
Console.WriteLine(name);
Console.WriteLine(value);
Output:
PropertyName
SomeValue
Error checking is left as an exercise for the reader...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With