Generic Interface Definition

Source: Internet
Author: User

Many problems have been encountered during the use of generic definition classes. The special record is as follows:

The most basic generic classes are defined as follows:


[Csharp] public abstract class GetDataBase <T>: IHttpHandler, IRequiresSessionState {
Protected abstract T GetModel (HttpContext context );
Protected abstract IList <T> GetList (int pageSize, int pageIndex, string where, string sortname, string sortorder, out int total );
Protected JsonFlexiGridData GetFlexiGridData (IList <T> list, int pageIndex, int pageSize, int total, string colkey, string colsinf)
{
PagedList <T> pl = new PagedList <T> ();
Pl. PageIndex = pageIndex-1;
Pl. PageSize = pageSize;
Pl. DataList = new List <T> ();
Pl. DataList. AddRange (list );
Pl. Total = total;
JsonFlexiGridData data = JsonFlexiGridData. ConvertFromPagedList (pl, colkey, colsinf. Split (','));
Return data;
}

}

Public abstract class GetDataBase <T>: IHttpHandler, IRequiresSessionState {
Protected abstract T GetModel (HttpContext context );
Protected abstract IList <T> GetList (int pageSize, int pageIndex, string where, string sortname, string sortorder, out int total );
Protected JsonFlexiGridData GetFlexiGridData (IList <T> list, int pageIndex, int pageSize, int total, string colkey, string colsinf)
{
PagedList <T> pl = new PagedList <T> ();
Pl. PageIndex = pageIndex-1;
Pl. PageSize = pageSize;
Pl. DataList = new List <T> ();
Pl. DataList. AddRange (list );
Pl. Total = total;
JsonFlexiGridData data = JsonFlexiGridData. ConvertFromPagedList (pl, colkey, colsinf. Split (','));
Return data;
}
 
}

 

 


In fact, the simplest thing is to add <T>, which indicates the generic class. pl can be used in the process. dataList = new List <T> (); always prompts errors, compilation fails, saying it must be a class, so modify as follows

[Csharp] public abstract class GetDataBase <T>: IHttpHandler, IRequiresSessionState where T: class {

Public abstract class GetDataBase <T>: IHttpHandler, IRequiresSessionState where T: class {1 set the generic base class or requirements


The key sentence where T: class indicates that the type is a class. Of course, if T is required to be another type, such as an interface, or inherit from a class, it is also the same modification method.

For example, the generic interface inherits from the generic interface IObjectWithKey <TK>,


[Csharp]
Public interface IDeviceAgent <TK, TCk>: IObjectWithKey <TK>, IDisposable {

Public interface IDeviceAgent <TK, TCk>: IObjectWithKey <TK>, IDisposable {

 

For example, the first type of TV of the generic interface IContainer must inherit from the interface IObjectWithKey <TK>
[Csharp]
Public interface IContainer <TK, TV> where TV: IObjectWithKey <TK> {

Public interface IContainer <TK, TV> where TV: IObjectWithKey <TK> {

 

 

2. There are multiple generic types.

[Csharp]
Public interface IContainer <TK, TV> where TV: IObjectWithKey <TK> {

Public interface IContainer <TK, TV> where TV: IObjectWithKey <TK> {has multiple types. Of course, the two types can be the same or different in a specific class.

In fact, multiple types are placed in a pair of <>. If there are several types, just put a few parameters. There are no special requirements for names.

 

 

3. If there are multiple type constraints for generics, such as class requirements, how to deal with them?


I tried it many times and don't know how to define it. Please give me some advice.


[Csharp]
Public abstract class GetDataBase <T, T2>: IHttpHandler, IRequiresSessionState where T2, T: class {

Public abstract class GetDataBase <T, T2>: IHttpHandler, IRequiresSessionState where T2, T: class {

 

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.