开发者

dynamic list of type or pattern to solve chaning type problem?

开发者 https://www.devze.com 2023-01-02 03:19 出处:网络
I have a class which uses a list for passing to ssrs and for each report I had to define a list of that type.

I have a class which uses a list for passing to ssrs and for each report I had to define a list of that type. like this

List<ReportClass1> ReportClass1prods;
public List<ReportClass1> GetReportClass1
{
    get
    {
        return ReportClass1prods;
    }
}
List<ReportClass2> ReportClass2prods;
public List<ReportClass2> GetReportClass2
{
    get
    {
        return ReportClass2prods;
    }
}

List<ReportClass3> ReportClass3prods;
public List<ReportClass3> GetReportClass3
{
    get
    {
        return R开发者_运维技巧eportClass3prods;
    }
}

and then each one has its own function

public void LoadReport1(List<ReportClass1> products)
{
    ReportClass1prods = products;
    reportViewer1.Clear();
    reportViewer1.Reset(); 
    reportViewer1.ProcessingMode = ProcessingMode.Local;
    reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
    reportViewer1.LocalReport.DataSources.Add(
            new ReportDataSource("ReportClass1", GetReportClass1));
    reportViewer1.RefreshReport();
}

Is there anyway/ what is the best way to make it so I dont need to copy and paste each list function for each new report but can dynamically figure out the type so there is only 1 list and 1 load function?


Check which common class or interface are ReportClass1,2,3 derived from (or if these are your classes, not some kind of generated ones, make it derive from a common base class or interface). Then use List<BaseClass>, e.g. List<ReportClass>.

0

精彩评论

暂无评论...
验证码 换一张
取 消