无论签名如何,使 Lambda 表达式成为委托目标

发布于 2024-09-02 09:31:18 字数 1740 浏览 4 评论 0原文

好吧,也许标题不是世界上最具描述性的东西,但这个问题快要了我的命。我想做的是创建一个 ActionLinkFor 辅助方法,如下所示:

public ActionResult Index()
{
   // Does Stuff
}

public ActionResult SomeAction(int param1)
{
   // Does Stuff
}

这是两个操作方法。操作方法可以有任意数量的参数,但出于我的表达目的,我不在乎。理想情况下,我一直在尝试的是这样的:

<%= Html.ActionLinkFor<HomeController>(m => m.Index, "Return Home") %>

返回

<a href="/Home/Index">Return Home</a>

它将根据它解析的表达式 。目标是现在我不再使用“魔术字符串”来确定链接,而是使用编译后的 lambda 表达式。如果重构任何内容,或者更改页面位置,那么哪些链接指向何处就会很清楚。

问题是,该方法句柄的 lambda 表达式似乎无法解析为正确的重载。我想我必须像这样进行一些重载:

public static MvcHtmlString ActionLinkFor<Controller>(this HtmlHelper helper, Expression<Func<Controller, ActionResult>> methodExpression, string linkName)
{
     // return parsed expression translated into a link
}

public static MvcHtmlString ActionLinkFor<Controller>(this HtmlHelper helper, Expression<Func<Controller, object, ActionResult>> methodExpression, string linkName)
{
    // Does the same thing
}

这样,当我说

m => m.Index // Chooses first ActionLinkFor since the Lambda would be a Func<Controller, object>
m => m.SomeAction // Chooses second ActionLinkFor since the method signature is different.

但是,这似乎不起作用。如果我像这样指定多个泛型参数:

<%= Html.ActionLinkFor<HomeController, int>(m => m.SomeAction, "Some Link") %>

它会很好地选择它,但我不想在泛型参数列表中指定方法签名。所以我的问题是:

1)有没有办法“定位”方法名称,而不管它的签名如何?我可以使用反射来获取所有方法并使用魔术字符串,但我希望在设计时有一个 lambda 表达式,以便我可以指定链接。

2)如果您能提供解决方案,是否有一种简单的方法来解析方法名称?我当前使用的算法很可能是一个坏主意,并且有效地涉及一些转换,然后当我无法将其转换为更方便的类型时最终解析字符串。

Ok, maybe the title isn't the most descriptive thing in the world, but this problem is killing me. What I'm trying to do is create an ActionLinkFor helper method, like so:

public ActionResult Index()
{
   // Does Stuff
}

public ActionResult SomeAction(int param1)
{
   // Does Stuff
}

These are two action methods. Action methods can have any number of parameters, but for the purposes of my expression, I don't care. Ideally, what I have been trying for is something like this:

<%= Html.ActionLinkFor<HomeController>(m => m.Index, "Return Home") %>

which would return

<a href="/Home/Index">Return Home</a>

based on the expression it parsed. The objective being that now I'm not using 'magic strings' for determining the links, but instead a compiled lambda expression. If anything was refactored, or a page was changed location, it would be clear what links had been pointing where.

The problem is, the lambda expression for that method handle doesn't seem to resolve to the right overload. I figured I would have to make a few overloads like so:

public static MvcHtmlString ActionLinkFor<Controller>(this HtmlHelper helper, Expression<Func<Controller, ActionResult>> methodExpression, string linkName)
{
     // return parsed expression translated into a link
}

public static MvcHtmlString ActionLinkFor<Controller>(this HtmlHelper helper, Expression<Func<Controller, object, ActionResult>> methodExpression, string linkName)
{
    // Does the same thing
}

That way, when I said

m => m.Index // Chooses first ActionLinkFor since the Lambda would be a Func<Controller, object>
m => m.SomeAction // Chooses second ActionLinkFor since the method signature is different.

However, this doesn't seem to be working. If I specify multiple generic parameters like so:

<%= Html.ActionLinkFor<HomeController, int>(m => m.SomeAction, "Some Link") %>

it picks it up just fine, but I don't want to have to specify the method signature in the generic argument list. So my questions are:

1) Is there a way to 'target' a method name regardless of it's signature? I can use reflection to get all the methods and use a magic string, but I want there to be a lambda expression at design time so I can specify the link then.

2) If you can provide a solution, is there a simple way of parsing out the method name? The algorithm I'm currently using is more than likely a bad idea, and effectively involves a few conversions and then eventually parsing a string when I couldn't get it to a more convenient type.

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

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

发布评论

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

评论(2

羞稚 2024-09-09 09:31:18

看起来我正在重新发明轮子。我将看一下 MVC Futures 代码。

It looks like I'm reinventing the wheel. I'll take a look at the MVC Futures code.

多像笑话 2024-09-09 09:31:18

ASP.Net MVC 2.0 Futures 中,您可以发现

Microsoft.Web.Mvc.LinkExtensions.ActionLink<TController>

:输入 ActionLink Helper。

In ASP.Net MVC 2.0 Futures you can find:

Microsoft.Web.Mvc.LinkExtensions.ActionLink<TController>

This is strongly typed ActionLink Helper.

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