汎用クラスインタフェース定義

2201 ワード

汎用定義クラスを使用する過程で多くの問題が発生し、以下のように特記されています.
最も基本的な汎用クラスを定義するには、次のようにします.
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;   
    }
	
}

実は最も簡単なのは追加するだけです
は、汎用クラスを表し、使用中に pl.DataList = new List();いつもエラーを提示して、コンパイルは通過しないで、クラスでなければならないと言って、そこで以下のように修正します
public abstract class GetDataBase<T> :IHttpHandler, IRequiresSessionState where T : class{

1汎用ベースクラスまたは要求の設定
重要なwhere T:classはタイプがクラスであることを示します.もちろん、Tがインタフェースなどの他のタイプである必要がある場合、またはクラスを継承する場合も、同じ修正方法です.
例えば汎用インタフェースは汎用インタフェースIObjectWithKeyに継承され、
public interface IDeviceAgent<TK, TCk> : IObjectWithKey<TK>, IDisposable{

例えば、汎用インタフェースIContainerの第1のタイプのTVは、インタフェースIObjectWithKeyを継承しなければならない.
public interface IContainer<TK, TV> where TV:IObjectWithKey<TK>{

2汎用型には複数のタイプがあります
public interface IContainer<TK, TV> where TV:IObjectWithKey<TK>{

複数のタイプがあります.もちろん、特定のクラスでは、この2つのタイプは同じでも、異なるでも構いません.
実は1対の<>に複数のタイプを配置し、いくつかのタイプがあれば、いくつかのパラメータを配置し、名前には特別な要求はありません.
3汎用型複数のタイプ制約がある場合、たとえばクラスが必要な場合、どのように処理しますか.
public abstract class GetDataBase<TListItem, TModel> : IHttpHandler, IRequiresSessionState
    where TListItem : class
    where TModel : class

Microsoftドキュメント