Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "partial" on a generic class

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 possible?

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

partial class ViewModelCollection<CarViewModel>
{
    ... special commands for car view model
}
like image 240
Michael Hilus Avatar asked Dec 17 '22 15:12

Michael Hilus


2 Answers

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
}
like image 121
Femaref Avatar answered Jan 03 '23 12:01

Femaref


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

like image 29
Charlie Salts Avatar answered Jan 03 '23 12:01

Charlie Salts