开发者

Pro ASP.net MVC Framework code question

开发者 https://www.devze.com 2023-01-21 02:33 出处:网络
The following line on page 110 of Pro ASP.NET MVC Framework does not make sense to me: string links = ((HtmlHelper)null).PageLinks(2, 3, i => \"Page\" + i);

The following line on page 110 of Pro ASP.NET MVC Framework does not make sense to me:

string links = ((HtmlHelper)null).PageLinks(2, 3, i => "Page" + i);

I don't understand what is the null doing in the above statement?

I was unable to do .. HtmlHelper.PageLinks(....)

Is the Page开发者_如何学GoLinks Method an extension method?


It's invoking an extension method on a null "instance" of an HtmlHelper. Probably there's no HtmlHelper in scope and the author can't be bothered to create one. The PageLinks method itself will not require a reference to an HtmlHelper, so effectively the author is passing null.

If you think about the signature of the extension method:

public static string PageLinks
    (this HtmlHelper helper, int val1, int val2, Func<someType,string> func)

It simply means that the parameter helper will be passed as null. It's a weird construct though, particularly in a book. It's got a really hacky smell to it.

The call could be restated (perhaps more clearly) as:

AuthorsHtmlExtensionsClass.PageLinks(null, 2, 3, i => "Page" + i)

Just reading though my MVC2 copy of the same book, (it's the end of the SportsStore application, right?), Steven has changed the code to:

HtmlHelper html=null;
...
html.PageLinks(...)

Which I suppose is a little clearer.

0

精彩评论

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