我有一個結構類似這樣的方法:幫助必要轉換方法泛型方法
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