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 {