3

I have a class

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
}

And I have a lambda expression of the Person type

Expression<Func<TModel, TProperty>> expression

Who contains this value

{model => model.Name}

How can I evaluate that lambda expression against an instance of Person, to extract the Name value attribute?

2 Answers 2

7

You can compile the expression into a delegate and pass in a Person object:

Func<Person, string> getName = expression.Compile();
string name = getName(person);
Sign up to request clarification or add additional context in comments.

Comments

0

Using Expression trees:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.