Sort of, although it may be more trouble than it is worth.
ASP.Net MVC frequently uses expressions to get at property info in a strongly-typed fashion. The expression doesn't necessarily get evaluated; instead, it is parsed for its metadata.
This isn't specific to MVC; I mention it to cite an established pattern in a Microsoft framework.
Here's a sample that gets a property name and value from an expression:
// the type being evaluated
public class Foo
{
public string Bar {
get;
set;
}
}
// method in an evaluator class
public TProperty EvaluateProperty<TProperty>( Expression<Func<Foo, TProperty>> expression ) {
string propertyToGetName = ( (MemberExpression)expression.Body ).Member.Name;
// do something with the property name
// and/or evaluate the expression and get the value of the property
return expression.Compile()( null );
}
You call it like this (note the expressions being passed):
var foo = new Foo { Bar = "baz" };
string val = EvaluateProperty( o => foo.Bar );
foo = new Foo { Bar = "123456" };
val = EvaluateProperty( o => foo.Bar );