2010-06-30 34 views
0

我有一個結構類似這樣的方法:幫助必要轉換方法泛型方法

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指令或 集引用?)

任何幫助將不勝感激,

馬克

回答

3

U是一個實際變量,變量不能用作通用參數,只能使用類型。

我不知道你是如何使用你的報告類型,但我會建議或者使他們都從中派生的基類型(類或接口)Report。然後,您可以使用更具體的Report類型作爲ObjectResult的通用參數。

interface Report 
{ 
    /* parameters that all reports should have */ 
} 

class Report1 : Report 
{ 
    /* implementation details... */ 
} 

class Report2 : Report 
{ 
    // ... 
} 

// more report types 

public ActionResult Get(int intId, NameValue nvWeekNumber) 
{ 
    return new ObjectResult<Report>(ReportManager.Instance.Get(intId, nvWeekNumber)); 
} 
+0

感謝您的詳細回覆 - 非常感謝。 – markpirvine 2010-06-30 18:13:21

1

如果我正確理解你的代碼,通用(靜態)方法將是無用的。

您正在基於方法參數(動態)返回一個類型的對象。

我會爲您的報告類創建一個共同的祖先/接口,並使用它來返回修改後的行爲的正確對象。

2

尼克說的是正確的。相反,你需要更多的內容,注意你甚至不需要使用泛型。

public abstract class ReportDB 
{ 
    public abstract object GetReport(int weekNumber); 
} 

/// Repeat for each Report Type 
public class Report1 : ReportDB 
{ 
    public override object GetReport(int weekNumber) {} 
} 

public object Get(ReportDB db, int intWeekNumber) 
{ 
    try 
    { 
     if(db != null) 
      return db.GetReport(intWeekNumber); 
    } 
    catch(Exception ex) 
    { 
     LogError(ex); 
    } 

     return null; 
} 

那麼你可以使用你的電話

public ActionResult Get(int intId, NameValue nvWeekNumber) 
{ 
    Type U = Utilities.GetReportType(intId); // returns a Report1, Report2 type etc... 
    // ReportManager.Instance.Get() should create a ReportDB instance. 
    return new ObjectResult<object>(ReportManager.Instance.Get(), nvWeekNumber)); 
} 
+0

我調整了我的代碼以遵循此模式 - 謝謝 – markpirvine 2010-06-30 18:13:53

0
public ActionResult Get(int intId, NameValue nvWeekNumber) 
{ 
    Type U = Utilities.GetReportType(intId); 
    Type objResultType = typeof (ObjectResult<>).MakeGenericType(U); 
    return Activator.CreateInstance(objResultType, new []{ReportManager.Instance.Get(intId, nvWeekNumber)}); 
} 

同樣的技術可以在其他的方法可以使用,但我不建議這樣做,因爲它是決定了輸出的整數,而不是一個通用的返回類型。