我有一个结构类似这样的方法:帮助必要转换方法泛型方法
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;
}
而不是使用对象返回类型我喜欢做这个的一般方法,但是我米不知道我会怎么做 - 任何人都可以提出一个好方法?
此方法由下面的代码称为:
public ActionResult Get(int intId, NameValue nvWeekNumber)
{
Type U = Utilities.GetReportType(intId);
return new ObjectResult<object>(ReportManager.Instance.Get(intId, nvWeekNumber));
}
的辅助函数GetReportType返回基于参数INTID报告(例如报告1,报告2,报告3,等等)的类型。当我尝试使用,而不是对象变量U我得到一个错误:
- 参数“1”:无法从“对象”到“U”
- 类型或命名空间 名“U”不能转换找到(你 缺少using指令或 集引用?)
任何帮助将不胜感激,
马克
感谢您的详细回复 - 非常感谢。 – markpirvine 2010-06-30 18:13:21