开发者

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜