我正在阅读 Pro MVC 2 书,并且有一个为 HtmlHelper 类创建扩展方法的示例。
这里的代码示例:
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl)
{
//Magic here.
}
这是一个示例用法:
[Test]
public void Can_Generate_Links_To_Other_Pages()
{
//Arrange: We're going to extend the Html helper class.
//It doesn't matter if the variable we use is null
HtmlHelper html = null;
PagingInfo pagingInfo = PagingInfo(){
CurrentPage = 2,
TotalItems = 28,
ItemsPerPage = 10
};
Func<int, String> pageUrl = i => "Page" + i;
//Act: Here's how it should format the links.
MvcHtmlString result = html.PageLinks(pagingInfo, pageUrl);
//Assert:
result.ToString().ShouldEqual(@"<a href=""Page1"">1</a><a href=""Page2"">2</a><a href=""Page3"">3</a>")
}
编辑:删除了混淆这个问题的部分。
问题是:为什么示例使用 Func?我应该什么时候使用它?什么是功能?
谢谢!