Related articles: teach you 30 seconds to build strong type ASP.net data binding, strongly typed ASP.net data binding improved edition
There is a problem with the previous approach:
Ivony ... "It's not good to write an eval method for each type," says one. ”
Kingthy said: "So there are func everywhere, will it be too far?" ”
"The methods to be used anywhere in the project should be collected in the core-level DLL as an extension method," Li Yongjing said. ”
If you use the extension method to solve this problem, we will face new problems, see extension methods:
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 ());
}
}
The problem is that when you call, you need to specify the return type, such as:
<%# this. Eval<newsinfo, string> (n => n.title)%>
There is another way to solve this problem with the following code:
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 calling code is as follows:
<%# Binder<newsinfo> Eval (this, n => n.title)%>
Is there a better solution? I can only find this method at present, I 211.html "> think the ideal calling method should be:
<%# eval<newsinfo> (n => n.title)%>