I have a method that is structured like this:
public object Get(int intId, int intWeekNumber)
{
try
{
if(intWeekNumber > -1)
{
switch(intId)
{
case 1:
return ReportDB.GetReport1(intWeekNumber);//return object of type Report1
case 2:
return ReportDB.GetReport2(intWeekNumber);//return object of type Report2
case 3:
return ReportDB.GetReport3(intWeekNumber);//return object of type Report3
case 4:
return ReportDB.GetReport4(intWeekNumber);//return object of type Report4
}
}
}
catch(Exception ex)
{
LogError(ex);
}
return null;
}
Rather than use object as the return type I like to make this a generic method, however I’m not sure how I would do it – can anyone suggest a good approach?
This method is called by the following code:
public ActionResult Get(int intId, NameValue nvWeekNumber)
{
Type U = Utilities.GetReportType(intId);
return new ObjectResult<object>(ReportManager.Instance.Get(intId, nvWeekNumber));
}
The helper function GetReportType returns the type of report (e.g. Report1, Report2, Report3, etc) based on the parameter intId. When I try to use variable U instead of object I get an errors:
- Argument ‘1’: cannot convert from ‘object’ to ‘U’
- The type or namespace
name ‘U’ could not be found (are you
missing a using directive or an
assembly reference?)
Any help would be greatly appreciated,
Mark
Uis an actual variable, and variables cannot be used as generic parameters, only types can.I am not sure how you are using your report types, but I would suggest either making a base type (class or interface)
Reportthat they all derive from. You could then use the more specificReporttype as the generic parameter to yourObjectResult.