生成表达式<func <t,object =“”>&gt;参数</FUNC <t时,>

时间:2012-02-15 20:40:08

标签: c# expression-trees

我有这样的方法:

public static MvcHtmlString Pager<T>(T urlParams, Expression<Func<T, object>> pageProperty) where T : class
{
        string pagingProp = Helpers.PropertyToString(pageProperty.Body);
        //set property on object using reflection.
        PropertyInfo prop = type.GetProperty(urlParams.GetType());

}

表达式的目的是知道urlParams的哪个属性应该用于分页。

让我说我有班级:

public class Pagination
{
    public int PageIndex {get; set; }
}

我想这样称呼:

Html.Pager(new Pagination{ PageIndex = 1 }, new Expression<Func<Pagination>>(p => p.PageIndex))

问题:Expression<Func<Pagination>>()确实需要构造函数,如何告诉表达式我想使用PageIndex属性?

1 个答案:

答案 0 :(得分:3)

假设:

void M(Expression<Func<User, object>> f) { /* ... some implementation ... */ }

然后:

M(u => u.Password);