C# 将方法转换为泛型方法所需的帮助

C# 将方法转换为泛型方法所需的帮助,c#,generics,C#,Generics,我有一个结构如下的方法: public object Get(int intId, int intWeekNumber) { try { if(intWeekNumber > -1) { switch(intId) { case 1: retur

我有一个结构如下的方法:

    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;
    }
我不想使用object作为返回类型,而是想让它成为一个通用的方法,但是我不知道该怎么做——有人能推荐一个好的方法吗

此方法由以下代码调用:

    public ActionResult Get(int intId, NameValue nvWeekNumber)
    {
        Type U = Utilities.GetReportType(intId);
        return new ObjectResult<object>(ReportManager.Instance.Get(intId, nvWeekNumber));
    }
public ActionResult Get(int intId,NameValue nvWeekNumber)
{
类型U=实用程序.GetReportType(intId);
返回新的ObjectResult(ReportManager.Instance.Get(intId,nvWeekNumber));
}
助手函数GetReportType根据参数intId返回报告类型(例如Report1、Report2、Report3等)。当我尝试使用变量U而不是对象时,我得到一个错误:

  • 参数“1”:无法从“对象”转换为“U”
  • 类型或命名空间 找不到名称“U”(您是 缺少using指令或 装配参考?)
任何帮助都将不胜感激


Mark

U
是实际变量,变量不能用作泛型参数,只能使用类型

我不确定您是如何使用报表类型的,但我建议您创建一个它们都源自的基本类型(类或接口)
report
。然后可以使用更具体的
报告
类型作为
对象结果
的通用参数

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));
}
接口报告
{
/*所有报告都应具有的参数*/
}
课堂报告1:报告
{
/*实施细节*/
}
课堂报告2:报告
{
// ...
}
//更多报告类型
公共操作结果获取(int intId,NameValue nvWeekNumber)
{
返回新的ObjectResult(ReportManager.Instance.Get(intId,nvWeekNumber));
}

U
是实际变量,变量不能用作泛型参数,只能使用类型

我不确定您是如何使用报表类型的,但我建议您创建一个它们都源自的基本类型(类或接口)
report
。然后可以使用更具体的
报告
类型作为
对象结果
的通用参数

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));
}
接口报告
{
/*所有报告都应具有的参数*/
}
课堂报告1:报告
{
/*实施细节*/
}
课堂报告2:报告
{
// ...
}
//更多报告类型
公共操作结果获取(int intId,NameValue nvWeekNumber)
{
返回新的ObjectResult(ReportManager.Instance.Get(intId,nvWeekNumber));
}

如果我正确理解了您的代码,那么通用(静态)方法将毫无用处

您正在基于方法参数(动态)返回类型为的对象


我将为您的报表类创建一个公共祖先/接口,并使用它返回具有修改行为的正确对象。

如果我正确理解您的代码,则通用(静态)方法将毫无用处

您正在基于方法参数(动态)返回类型为的对象


我将为您的报表类创建一个公共祖先/接口,并使用它返回行为经过修改的正确对象。

Nick所说的是正确的。相反,您需要更符合以下内容的东西,请注意,您甚至不需要使用泛型

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));
}
public ActionResult Get(int intId,NameValue nvWeekNumber)
{
类型U=Utilities.GetReportType(intId);//返回Report1、Report2类型等。。。
//ReportManager.Instance.Get()应创建一个ReportDB实例。
返回新的ObjectResult(ReportManager.Instance.Get(),nvWeekNumber));
}

尼克说的是对的。相反,您需要更符合以下内容的东西,请注意,您甚至不需要使用泛型

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));
}
public ActionResult Get(int intId,NameValue nvWeekNumber)
{
类型U=Utilities.GetReportType(intId);//返回Report1、Report2类型等。。。
//ReportManager.Instance.Get()应创建一个ReportDB实例。
返回新的ObjectResult(ReportManager.Instance.Get(),nvWeekNumber));
}
公共操作结果获取(int intId,NameValue nvWeekNumber)
{
类型U=实用程序.GetReportType(intId);
类型objResultType=typeof(ObjectResult);
返回Activator.CreateInstance(objResultType,new[]{ReportManager.Instance.Get(intId,nvWeekNumber)});
}
同样的技术也可以在另一个方法上使用,但我不建议使用它,因为它是指示输出的整数,而不是通用的返回类型。

public ActionResult Get(int intId,NameValue nvWeekNumber)
{
类型U=实用程序.GetReportType(intId);
类型objResultType=typeof(ObjectResult);
返回Activator.CreateInstance(objResultType,new[]{ReportManager.Instance.Get(intId,nvWeekNumber)});
}

同样的技术也可以用在另一种方法上,但我不推荐使用它,因为它是决定输出的整数,而不是一般的返回类型。

感谢您的详细回复-非常感谢。感谢您的详细回复-非常感谢。