Trying to use Generics for code optimization. The program works however, due to redundant lines of code, I thought to try Generics once and check if the number of lines of codes can be reduced. I'd appreciate your help and feedbacks.
The error is type does not contain definition of Document
Print.cs
public class Print
{
public async Task<ResponseData> Get(Payload payload)
{
var FilePath = "";
try
{
MemoryStream memstream = new MemoryStream();
if (payload?.Type?.ToLower() == "consolidated")
{
if (payload?.Month?.ToLower() == "july" || payload?.Year == "2022")
{
if (payload?.date != "")
{
if (payload?.PageID?.IndexOf(",") > -1)
{
AReport aReport = Adjustments.FillAReport(payload);
PdfExport pdf = new PdfExport();
FilePath = $"{Guid.NewGuid()}.pdf";
pdf.Export(aReport.Document, filePath);
}
else
{
BReport bReport = Adjustments.FillBReport(payload, true);
PdfExport pdf = new PdfExport();
FilePath = $"{Guid.NewGuid()}.pdf";
pdf.Export(bReport.Document, FilePath);
}
}
else
{
CReport cReport = Adjustments.FillCReport(payload);
PdfExport pdf = new PdfExport();
FilePath = $"{Guid.NewGuid()}.pdf";
pdf.Export(cReport.Document, FilePath);
}
}
}
else
{
BReport bReport = Adjustments.FillBReport(payload, false);
PdfExport pdf = new PdfExport();
FilePath = $"{Guid.NewGuid()}.pdf";
pdf.Export(bReport.Document, FilePath);
}
}
Trying to Use Generics here. After Export is completed, I am trying to return back PDF file generated.
if (payload?.PageID?.IndexOf(",") > -1)
{
AReport aReport = Adjustments.FillAReport(payload);
FilePath = Generics.ExportContent<AReport>(aReport )
}
else
{
BReport bReport = Adjustments.FillBReport(payload, true);
FilePath = Generics.ExportContent<BReport>(bReport)
}
Generics.cs
public static string ExportContent<T>(this T type)
{
PdfExport pdf = new PdfExport();
FilePath = $"{Guid.NewGuid()}.pdf";
pdf.Export(type.Document, FilePath); //Error
return FilePath;
}