2010-06-30 23 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)}); 
} 

同样的技术可以在其他的方法可以使用,但我不建议这样做,因为它是决定了输出的整数,而不是一个通用的返回类型。