I’d like to do something as the follows, using lambdas and LINQ expressions in C#.
public class Foo
{
private SomeEntity entity;
public Foo(SomeEntity entity)
{
this.entity = entity;
Bar(p => p.FirstName);
Bar(p => p.SurName);
}
public void Bar(Expression> barExpression)
{
// What to do here?
}
}
Every time I call Bar(), I’d like to dig into the Expression, and find out which property I am referring to (e.g. FirstName or LastName), and also which SomeEntity object I am currently working with. I also need the value of the property (this is in fact the most important).
Eventually I’d like to extend Bar to do more than this, but this is as simple as I can “boil” my experiment.
Thanks!
should get the name; there is no instance in
p => p.FirstName; butthis.entityshould give you what you want. The value isT value = expression.Compile()(entity)