Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在方法调用中扩展泛型类_C#_Generics - Fatal编程技术网

C#在方法调用中扩展泛型类

C#在方法调用中扩展泛型类,c#,generics,C#,Generics,我试图编写一个传递泛型参数的方法,该泛型参数将使用泛型参数扩展类,但我不希望必须向该方法传递第二个泛型参数。有没有一种方法可以在不传递两个通用参数的情况下实现我想要做的事情 // this base doesn't work because it expects a BaseReportViewModel<D> // I don't want to have to pass in two generic arguments from the child controller //

我试图编写一个传递泛型参数的方法,该泛型参数将使用泛型参数扩展类,但我不希望必须向该方法传递第二个泛型参数。有没有一种方法可以在不传递两个通用参数的情况下实现我想要做的事情

// this base doesn't work because it expects a BaseReportViewModel<D> 
// I don't want to have to pass in two generic arguments from the child controller
// this is what I don't want to do: this.ReportView<Report1ViewModel, Report1Data>(viewModel);
// I want to ONLY have to pass in a single generic argument. 

public class BaseController {
  // the below line won't compile because it expects a generic type to be passed to BaseReportViewModel<>
  public ActionResult ReportView<T>(T viewModel) where T : BaseReportViewModel {
    viewModel.ReportGroup = this.ReportGroup;
    viewModel.ReportTitle = this.ReportTitle;
    return this.View(viewModel);
  }
}

public class ChildController {  
  var viewModel = new Report1ViewModel();               
  viewModel.Data = new Report1Data();
  return this.ReportView<Report1ViewModel>(viewModel);            
}

public class BaseReportViewModel<T> : BaseViewModel where T : ReportBaseData {
  public string ReportGroup { get; set; }
  public string ReportTitle { get; set; }
  public T Data { get; set; }
}

public class Report1ViewModel : BaseReportViewModel<Report1Data> {
}

public class Report1Data : BaseReportData {
}
//此基不起作用,因为它需要BaseReportViewModel
//我不希望必须从子控制器传入两个通用参数
//这是我不想做的:this.ReportView(viewModel);
//我只想传入一个泛型参数。
公共类基址控制器{
//下一行将不会编译,因为它需要将泛型类型传递给BaseReportViewModel
public ActionResult ReportView(T viewModel),其中T:BaseReportViewModel{
viewModel.ReportGroup=this.ReportGroup;
viewModel.ReportTitle=this.ReportTitle;
返回此.View(viewModel);
}
}
公共类ChildController{
var viewModel=new Report1ViewModel();
viewModel.Data=newreport1data();
返回此.ReportView(viewModel);
}
公共类BaseReportViewModel:BaseViewModel,其中T:ReportBaseData{
公共字符串报告组{get;set;}
公共字符串ReportTitle{get;set;}
公共T数据{get;set;}
}
公共类Report1ViewModel:BaseReportViewModel{
}
公共类Report1Data:BaseReportData{
}

我想你想要这样的东西

public ActionResult ReportView<T>(BaseReportViewModel<T> viewModel) 
    where T : ReportBaseData 
公共操作结果报告视图(BaseReportViewModel viewModel) 其中T:ReportBaseData
旁注:您也不需要在
this.ReportView(viewModel)中传递type调用,只需
ReportView(viewModel)应该足够了,因为类型应该从参数派生。

如果我以BaseReportViewModel的形式传入对象,该方法会丢失子类型吗?这意味着我需要将数据对象的类型传递到ReportView方法中?现在尝试一下。。。我想这可能行得通!我很快会给出反馈。@adamlevit如果您执行
BaseViewModel m=new BaseReportViewModel()
,则您已经丢失了子类型的编译时信息,并且泛型代码将无法找到正确的类型(因为这是编译时操作)。如果您只是简单地使用
var m=Report1ViewModel()
,那么应该没问题。是的,我使用的是var。通过这种方法,我如何在razor@model声明中引用扩展泛型类型?@adamlevit不确定您的问题是什么。。。您是否询问是否可以使用
@model Report1ViewModel
@model BaseReportViewModel
?或者您正试图创建一个接受所有派生类的页面(如not legal
@model BaseReportViewModel
)?