开发者

Using "partial" on a generic class

开发者 https://www.devze.com 2023-02-26 08:07 出处:网络
I am using a generic class called ViewModelCollection<BaseViewModel> which handles a lists of ViewModels and delivers standard add() and delete() commands.

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

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

Is something like this po开发者_C百科ssible?

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

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


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
}


partial is used only to split a class across multiple source files. The class definition itself must be the same.


Take the partial methods added and create an interface, you can then constrain the generic to use that interface and work off of those methods defined.

0

精彩评论

暂无评论...
验证码 换一张
取 消