If I have an abstract class, is it possible to somehow return an enumerator of the type of the derived class? Or will I have to use generics in a base class or a generic method? Here's a really dumb example of what I'm trying to do -
public abstract class Person { public IEnumerable<MyType> Search() { DbDataReader reader = Database.Instance.ExecuteReader(sql); while(reader.Read()) { MyType row = new MyType(); row.Load(reader); yeild return row; } } private Load(DbDataReader reader) {
Then in another place I would like to call
Programmer programmer = new Programmer(); programmer.Location = "My city"; programmer.Language = "C#"; foreach(Programmer programmer in programmer.Search()) { //display list of c
I know that I can do this using a general method, for example, Search<T>() , but I would like to be able to call the search function from a class that does not know exactly what the Person type is (for example, the base class for the AJAX handler)
If this is not possible, can someone give me an example or reason why not? Or will it be too difficult to implement in the compiler?
source share