Generating expression tree for a subproperty test
I want to generate a LambdaExpression for a statement like this :
Adress (p => 开发者_StackOverflowp.Person.Name == "Mike")
where Person
is a class which have a Name property.
I can't achieve this with Expression.Property
.
Any ideas ?
Assuming the type of p being PersonContainer:
// p => p.Person.Name == "Mike"
ParameterExpression par = Expression.Parameter(typeof(PersonContainer), "p");
BinaryExpression beEq = Expression.Equal(
Expression.Property(
Expression.Property(par, "Person"),
"Name"),
Expression.Constant("Mike"));
Expression<Func<PersonContainer, bool>> expr = Expression.Lambda<Func<PersonContainer, bool>>(beEq, par);
精彩评论