使用“部分"在通用类上 [英] Using "partial" on a generic class

查看:96
本文介绍了使用“部分"在通用类上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在使用名为ViewModelCollection<BaseViewModel>的通用类,该类处理ViewModel的列表并提供标准的add()delete()命令.

Hey guys,
I'm using a generic class called ViewModelCollection<BaseViewModel> which handles a lists of ViewModels and delivers standard add() and delete() commands.

现在,我想知道是否可以使用partial构造针对某个ViewModel(名称为CarViewModel)来扩展"此类.

Now I'm wondering if I can "extend" this class using the partial construct for a certain ViewModel, whose name is, say, CarViewModel.

这样可能吗?

partial class ViewModelCollection<BaseViewModel>
{
    ... some command and list stuff ...
}

partial class ViewModelCollection<CarViewModel>
{
    ... special commands for car view model
}

推荐答案

不,您不能,partial只是将类定义拆分为多个文件,定义必须相同.您需要从ViewModelCollection<T>派生:

No, you can't, partial just splits the class definition over multiple files, the definition has to be the same. You need to derive from ViewModelCollection<T>:

public class ViewModelCollection<T> where T: BaseViewModel
{
   //methods
}

public class CarViewModelCollection : ViewModelCollection<CarVieModel>
{
  //specific methods
}

这篇关于使用“部分"在通用类上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆