I have base classes:
public class AbstractData { public int ID { get; set; } } public class Person: AbstractData { public string Name { get; set; } } public class AbstractManager<T> where T: AbstractData { public virtual List<T> GetAll() { } public virtual T GetOne(int id) { } } public class PersonManager: AbstractManager<Person> { public override List<Person> GetAll() {
Now I have a Windows Forms base class, for example:
public class BaseForm: Form { public virtual AbstractManager<T> GetManager<T>() where T: AbstractData { return null; } }
and derivative form:
public class PersonForm: BaseForm { public override AbstractManager<T> GetManager<T>() { return new PersonManager(); } }
The problem is that I keep getting compilation errors of the PersonForm class:
Cannot implicitly convert type 'PersonManager' to 'AbstractManager <T>'
Is there a way I can create this virtual method and each class derived from BaseForm returns a specific AbstractManager view?
If I get rid of generic in the AbstractManager class, then I compile OK (with a few code changes), but then the GetAll method cannot return List<T> . It would have to return List<AbstractData> instead, which causes problems when converting from List<Person> to List<AbstractData> .
Any help would be appreciated.
generics methods c # virtual
user3215491
source share