如何使这种通用

时间:2018-06-22 20:05:19

标签: c# .net generics constructor wpfdatagrid

我想使用实例<T>的构造函数将该类作为通用类,这是我的类。

class Pagenation
{
    public TextBlock PageInformation { get; set; }
    public DataTable dt              { get; set; }
    public ComboBox NumberOfRecords  { get; set; }
    public DataGrid DatagridObject   { get; set; }
    public Button BtnPrev            { get; set; }
    public Button BtnFirst           { get; set; }
    public Button BtnNext            { get; set; }
    public Button BtnLast            { get; set; }
    public int NumberOfRecPerPage    { get; set; }

    private int pageIndex = 1;
    private int numberOfRecPerPage;

    public Pagenation(DataGrid UCDatagrid, ComboBox NumberOfRecordsComboBox, int 
                      numOfRecPerPage, TextBlock PageInfo, DataTable Data, 
                      Button Prev, Button First, Button Next, Button Last)
    {
        NumberOfRecordsComboBox.Items.AddRange(new List<string>{ "5", "10", "15", "20" });

        (NumberOfRecords, numberOfRecPerPage, DatagridObject) = 
            (NumberOfRecordsComboBox, numOfRecPerPage, UCDatagrid);

        dt = Data.Copy();

        PageInformation = PageInfo;
        (BtnPrev, BtnFirst, BtnNext, BtnLast) = (Prev, First, Next, Last);
    }
}

我希望Pagenation构造函数接收类型为<some project Custom Class>的对象,如下所示

Pagenation PG = new Pagenation<PartsUC>(this);

我做了这样的通用类

class Pagenation<T>
{
    public Pagenation(T ClassObj)
    {
        NumberOfRecords    = ClassObj.???;  //  want to assign the properties of the ClassObj
        numberOfRecPerPage = ClassObj.???;
        DatagridObject     = ClassObj.???;

        dt = ClassObj.???;

        PageInformation =ClassObj.???;

        (BtnPrev, BtnFirst, BtnNext, BtnLast) = 
            (ClassObj.???, ClassObj.???, ClassObj.???, ClassObj.???);
    }
}

我不知道如何将分页类属性重新分配给接收到的<PartsUC>实例对象的属性

1 个答案:

答案 0 :(得分:3)

在类名后面添加where T : PartsUC,这将限制您的泛型类型。

class Pagenation<T> where T : PartsUC
{

    public Pagenation(T ClassObj)
    {

        this.NumberOfRecords = ClassObj.NumberOfRecords;
        this.numberOfRecPerPage = ClassObj.numberOfRecPerPage;
        this.DatagridObject = ClassObj.???????;
        //... set your property from PartsUC class
    }
}

Generic where

编辑 使 interface abstract 成为那些类合同,然后where: <your interface>

您的类实现IUC接口,然后封装自己的属性。

public interface IUC{
    Button BtnPrev { get; set;}
    Button BtnFirst{ get; set;}
    Button BtnNext{ get; set;}
    Button BtnLast{ get; set;}
    DataGrid DatagridObject{ get; set;}
    //... all you need property info
}

public class PartsUC : IUC{
    Button BtnPrev { get; set;}
    Button BtnFirst{ get; set;}
    Button BtnNext{ get; set;}
    Button BtnLast{ get; set;}
    DataGrid DatagridObject{ get; set;}
    //... all you need property info
}

public class EqupmentUC : IUC{
    Button BtnPrev { get; set;}
    Button BtnFirst{ get; set;}
    Button BtnNext{ get; set;}
    Button BtnLast{ get; set;}
    DataGrid DatagridObject{ get; set;}
    //... all you need property info
}

public class UsersUC : IUC{
    Button BtnPrev { get; set;}
    Button BtnFirst{ get; set;}
    Button BtnNext{ get; set;}
    Button BtnLast{ get; set;}
    DataGrid DatagridObject{ get; set;}
    //... all you need property info
}


class Pagenation<T> where T : IUC
{

    public Pagenation(T ClassObj)
    {

        this.NumberOfRecords = ClassObj.NumberOfRecords;
        this.numberOfRecPerPage = ClassObj.numberOfRecPerPage;
        //... set your property from PartsUC class
    }
}