I'm getting the following compiler error
Cannot convert type 'T' to 'ProjectReportingHandler'
for the line
var projectReport = (ProjectReportingHandler)result.Report;
when trying to compile:
public abstract class ReportingHandler
{
// Report stuff
}
public abstract class ProjectReportingHandler: ReportingHandler
{
// Report stuff
// Project specific report stuff
}
public class ReportInstance<T>
where T : ReportingHandler
{
public T Report { get; private set; }
}
public class ReportLibraryEntry<T>
where T : ReportingHandler
{
public ReportInstance<T> Create()
{
ReportInstance<T> result = new ReportInstance<T>();
if (result.Report is ProjectReportingHandler)
{
var projectReport = (ProjectReportingHandler)result.Report;
// do stuff with project reports
}
return result;
}
}
Any ideas how to cast the linked generic type property result.Report
to a ProjectReportingHandler
?
I would have thought the where T : ReportingHandler
would have ensured this was possible :(
EDIT: I seem to be getting a few responses that say my accepted answer is incorrect. It works, and I implemented it as follows:
public ReportInstance<T> Create()
{
ReportInstance<T> result = new ReportInstance<T>();
ReportingHandler report = result.Report;
if (report is ProjectReportingHandler)
{
var projectReport = (ProjectReportingHandler)report;
// do stuff with project reports
}
return result;
}
Why the down votes for an answer that worked? :(