Strongly typed asp.net data binding improved version 2nd

Source: Internet
Author: User
Keywords This extended Sinfo resolved should
Tags .net asp asp.net class create data net project

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)%>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.