在使用VB.NET的ASP.NET MVC视图中使用Lambda表达式 [英] Use Lambda Expression within ASP.NET MVC View using VB.NET

查看:96
本文介绍了在使用VB.NET的ASP.NET MVC视图中使用Lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET MVC 1.0,.NET 3.5和C#,您可以轻松地向方法传递一个lambda表达式,该表达式在方法中执行时将执行"Response.Write"某些内容:

With ASP.NET MVC 1.0, .NET 3.5 and C# you can easily pass a method a lambda expression that will do a "Response.Write" some content when it's executed within the method:

<% Html.SomeExtensionMethod(
    () => { <%
        <p>Some page content<p>
    %> }
) %>

方法签名与此类似:

public void SomeExtensionMethod(this HtmlHelper helper, Action pageContent)
{
    pageContent();
}

有人知道如何使用VB.NET中的Lambda表达式,使用与上面所示相同的方法签名来执行类似的调用吗?

Does anyone know how to peforma a similar call using Lambda expressions in VB.NET using the same method signature shown above?

我已经在VB.NET中尝试了以下方法,但是它不起作用:

I've tried the following in VB.NET, but it wont work:

<% Html.SomeExtensionMethod(New Action( _
    Function() %> <p>Some Content</p> <% _
    )) %>

我得到一个例外,上面写着期望表达."

I get an exception saying "Expression Expected."

有人可以帮我解决我做错的事情吗?您如何在VB.NET中做到这一点?

Can anyone help me with what I'm doing wrong? How do you do this in VB.NET?

推荐答案

VB.NET 9.0不支持多语句lambda表达式或匿名方法.它还不支持不返回值含义的lambda表达式您不能声明类型为操作的匿名函数.

VB.NET 9.0 doesn't support multi-statement lambda expressions or anonymous methods. It also does not support lambda expression that does not return a value meaning that you cannot declare an anonymous function of type Action.

否则,这是可行的,但由于lambda不执行任何操作而没有意义:

Otherwise this works but is meaningless because the lambda does nothing:

<% Html.SomeExtensionMethod(New Action(Function() "<p>Some Content</p>"))%>

现在让我们举个例子:

<% Html.SomeExtensionMethod(New Action( _
    Function() %> <p>Some Content</p> <% _
)) %>

应该翻译成这样:

Html.SomeExtensionMethod(New Action(Function() Html.ViewContext.HttpContext.Response.Write("<p>Some Content</p>")))

由于表达式不返回值,因此无法在VB.NET 9.0中编译.

which doesn't compile in VB.NET 9.0 because your expression doesn't return a value.

在VB.NET 10中您将能够使用Sub关键字:

In VB.NET 10 you will be able to use the Sub keyword:

Html.SomeExtensionMethod(New Action(Sub() Html.ViewContext.HttpContext.Response.Write("<p>Some Content</p>")))

可以正常编译.

这篇关于在使用VB.NET的ASP.NET MVC视图中使用Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆