生成表达式>论点

发布于 2025-01-06 03:37:01 字数 797 浏览 5 评论 0 原文

我有一个这样的方法:

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>() 确实需要一个构造函数,我如何告诉我想要使用的表达式PageIndex 属性?

I have a method like this:

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

}

The purpose of the expression is to know what property of urlParams is the one that should be used for paging.

Lets say I have the class:

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

I would like to call it like this:

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

Problem: Expression<Func<Pagination>>() does take a constructor, how do I tell the expression I want to use the PageIndex property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2025-01-13 03:37:02

假设:

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

那么:

M(u => u.Password);

Assuming:

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

Then:

M(u => u.Password);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文