There is a problem with the previous method:
Ivony... said: "It is uncomfortable to write an Eval method for each type ."
Kingthy said, "Will there be too many Func everywhere ?"
Li yongjing said: "The methods used anywhere in the project should be collected and written as extension methods in Core-level Dll ."
If you use the extension method to solve this problem, we will face new problems. See the extension method:
public static class Binder
{
public static TResult Eval<TEntity, TResult>(this System.Web.UI.Page p,
Func<TEntity, TResult> func)
{
return func((TEntity)p.GetDataItem());
}
}
This problem occurs when calling, you need to specify the return type, for example:
<%# this.Eval<NewsInfo, string>(n => n.Title) %>
You can use another method to solve this problem. The Code is as follows:
public static class Binder<TEntity> where TEntity : class
{
public static TResult Eval<TResult>(System.Web.UI.Page p,
Func<TEntity, TResult> func)
{
return func((TEntity)p.GetDataItem());
}
}
The call code is as follows:
<%# Binder<NewsInfo>.Eval(this, n => n.Title) %>
Is there a better solution? Currently, I can only find this method. I think the ideal call method should be:
<%# Eval<NewsInfo>(n => n.Title)%>