mercredi 8 novembre 2017

How to get Expression's Object.Value with Reflection in C#

I want to get the value of a list. My method like this:

 private void predicateTest(Expression<Func<bool>> predicate)
        {
            var body = predicate.Body;

            var binaryExpression = body as BinaryExpression;
            var binaryBody= binaryExpression ??    
                        Expression.MakeBinary(ExpressionType.Equal, 
                        body, Expression.Constant(true));
            var left = binaryBody.Left;
        }

And my calling:

List<string> list = new List<string> { "doc", "exe", "jpg" };

predicateTest(() => list.Contains("sdf"));

And predicate.Body.Left value like following picture. How to get value of list as programmatic in predicateTest method?

enter image description here





Aucun commentaire:

Enregistrer un commentaire