Your problem is not very clear but I believe you can use the keyword new
instead of overwriting the method:
namespace ClassLibrary1
{
public interface IObjetosBase
{
IList<IObjetosBase> get();
}
public class ObjetosBase : IObjetosBase
{
public virtual IList<IObjetosBase> get() { return new List<IObjetosBase>(); }
}
public class Aluno : ObjetosBase
{
public new IList<Aluno> get()
{
return new List<Aluno>();
}
}
}
I could not test the code now for lack of time, but I also believe that the Aluno
class should not return a list of Aluno
, Maybe a Turma
should return a list of Aluno
or else , a% repository of%.
Edit:
Or You can populate the list of Alunos
with objects of type IObjetosBase
:
public class Aluno : ObjetosBase
{
public override IList<IObjetosBase> get()
{
List<IObjetosBase> lista = new List<IObjetosBase>();
Aluno obj;
for (int i = 0; i < 10; i++)
{
obj = new Aluno();
lista.Add(obj);
}
return lista;
}
}
And then to scroll through objects of type Aluno
:
public void Processo()
{
IList<IObjetosBase> lista = this.get();
//Se for necessário checar o tipo do objeto:
foreach (var obj in lista)
{
if (obj is Aluno)
{
Aluno a = obj as Aluno;
}
}
//Se não for necessário checar o tipo do objeto:
foreach (Aluno obj in lista)
{
}
}