This answer to a similar question gives an implementation of Label
. The code's by Josh Smith in the PropertyObserver class of his MVVM foundation:
private static string GetPropertyName
(Expression<Func<TPropertySource, object>> expression)
{
var lambda = expression as LambdaExpression;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = lambda.Body as UnaryExpression;
memberExpression = unaryExpression.Operand as MemberExpression;
}
else
{
memberExpression = lambda.Body as MemberExpression;
}
Debug.Assert(memberExpression != null,
"Please provide a lambda expression like 'n => n.PropertyName'");
if (memberExpression != null)
{
var propertyInfo = memberExpression.Member as PropertyInfo;
return propertyInfo.Name;
}
return null;
}
It works by looking at the expression tree and checking for the name of the property in member expressions.
For Editor
, you can use a similar strategy of looking through the Expression
to find out what you need about the property. What exactly to do depends a lot on what info you want.
The specific way you asked it where all you want is the value of a property from a Person
, you can simplify a lot. I also added a Person
parameter, since you seem to want the value for a given person.
int Editor(Person person, Func<Person, int> expression)
{
return expression(person);
}
This can be used like Editor(p, p => p.Id);
. Note that I changed Expression<Func<Person, int>>
to Func<Person, int>
, which means that instead of an expression tree it gets a Func
. You can't examine a Func
to find names of properties and such, but you can still use it to find the property from the Person
.