Brandon Tull
Brandon Tull

Reputation: 335

c# dynamic instantiation using reflection

I am exporting a report and I have noticed that I am copying a lot of code for each report. I would like to put it in a method in a separate class, but I am unsure of how to do the instantiation piece after some research. My code is below:

    ActiveReport rpt = new Reports.rptContractListing_Merchant();
    rpt.Run();
    try
    {
        rpt.Run(false);
    }
    catch (DataDynamics.ActiveReports.ReportException eRunReport)
    {
        // Failure running report, just report the error to the user:
        Response.Clear();
        Response.Write("<h1>Error running report:</h1>");
        Response.Write(eRunReport.ToString());
        return;
    }
    XlsExport xls = new XlsExport();
    xls.MinColumnWidth = (float)0.5;
    xls.Export(rpt.Document, m_stream);
    m_stream.Position = 0;
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("content-disposition", "inline;    filename=ContractListing_Merchant.xls");
    System.IO.MemoryStream m_stream = new System.IO.MemoryStream();
    Response.BinaryWrite(m_stream.ToArray());
    Response.End();

Here is the part I am unsure with the reflection:

ActiveReport rpt = new Reports.rptContractListing_Merchant();

Another Example:

    ActiveReport rpt = new Reports.rptContractDetails();
    try
    {
        rpt.Run(false);
    }
    catch (DataDynamics.ActiveReports.ReportException eRunReport)
    {
        // Failure running report, just report the error to the user:
        Response.Clear();
        Response.Write("<h1>Error running report:</h1>");
        Response.Write(eRunReport.ToString());
        return;
    }

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "inline; filename=ContractDetails.pdf");

    PdfExport pdf = new PdfExport();
    System.IO.MemoryStream memStream = new System.IO.MemoryStream();
    pdf.Export(rpt.Document, memStream);
    Response.BinaryWrite(memStream.ToArray());
    Response.End();

Upvotes: 2

Views: 671

Answers (3)

Brandon Tull
Brandon Tull

Reputation: 335

I just passed the rpt object into a method with the code in it. It worked.

Upvotes: 0

Duncan Howe
Duncan Howe

Reputation: 3025

I think Activator.CreateInstance<T>() is the way to go where T is the Type of report you are generating

Upvotes: 3

StriplingWarrior
StriplingWarrior

Reputation: 156524

You have a number of options, many of which don't even require reflection.

If you can guarantee that all your report classes have a "default" constructor (with no parameters), you can use generics and specify that the generic type must have a default constructor. Then you can just say new T().

Otherwise, you could either use dependency injection to create the report based on a given type, or you could have your method take the Report itself as an argument (which must be provided by the calling method), and occupy itself with the remaining, repetitive code.

Upvotes: 1

Related Questions