I created an ExpressionVisitor implementation that overrides VisitConstant. However, when I create an expression that utilizes a local variable I can't seem to get the actual value of the variable.
public class Person
{
public string FirstName { get; set; }
}
string name = "Michael";
Expression<Func<Person, object>> exp = p => p.FirstName == name;
How in the world do I get the value of the variable "name" from the ConstantExpression? The only thing that I can think of is this:
string fieldValue = value.GetType().GetFields().First().GetValue(value).ToString();
Obviously this doesn't lend itself to being very flexible though....
A slightly more complicated example would be the following:
Person localPerson = new Person { FirstName = "Michael" };
Expression<Func<Person, object>> exp = p => p.FirstName == localPerson.FirstName;