2

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(); 
6
  • You mean you want a function that accepts a Type object of Reports.rptContractListing_Merchant, instiates one then runs the rest of this function? Commented May 24, 2011 at 16:00
  • You may want to show us the code from two of the reports, so we can see which portions of it are repetitive, and can give you code examples of the best way to refactor it. This would also be appropriate for refactormycode.com Commented May 24, 2011 at 16:06
  • @rup Yes, that is what I'm looking to do. Commented May 24, 2011 at 16:08
  • 2
    I have an idea. Maybe I can just pass the rpt object into the method. Commented May 24, 2011 at 16:22
  • :-/ Can't believe I didn't think of that. However, another point whilst I'm here: if you're also going to pass in a filename to change the ContentDisposition header too then I'd recommend you use the System.Net.Mime.ContentDisposition class to generate the string since it will property escape the filename emitted in the header. And if you're actually using ASP.NET MVC then there's a return File(...) method on the controller that'll accept the stream and a filename and do this all for you. Commented May 24, 2011 at 16:27

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was trying to do, but I couldn't figure out the correct syntax for it.
1

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.

1 Comment

I'd guess they all inherit from ActiveReport. So, as Brandon's commented himself, he can just pass an ActiveReport into his function.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.