Estoy leyendo el libro Pro MVC 2, y hay un ejemplo de cómo crear un método de extensión para la clase HtmlHelper.¿Alguien puede explicar lo que hace el C# "Func <T,T>"?
Aquí el ejemplo de código:
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl)
{
//Magic here.
}
Y aquí es un ejemplo de uso:
[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>")
}
Editar: parte retirada que confundió el puntode esta pregunta.
La pregunta es: ¿Por qué el ejemplo usa Func? ¿Cuándo debería usarlo? ¿Qué es Func?
Gracias!
Principalmente, me gustaría saber cuál es el Func PAGEURL = i => "Page1" + i; línea está haciendo. –
No entiendo. 'Func' no tiene nada que ver con los métodos de extensión. ¿Que estas preguntando? –
BoltClock
¿Te refieres a la declaración de 'PageLinks'? El primer parámetro es 'this', es decir, es un método de extensión. Ese parámetro se une al objeto 'html' en la llamada. – Pete