dimanche 8 novembre 2015

Resolving Generic Covariance with a desirable Object Oriented Design, if it need

I have this code and I have a compile error because of the Covariance. How can I resolve this problem, I need my SelectAll method return IList<Entity>.

I want Some of my Entities inherit of an Abstract class and in the other classes, I could use the abstract class to do things. What is the best way to change my design to achieve this?

public interface IEntities
{    
    IList<IEntities> SelectAll();
}

public abstract class Entity : IEntities
{
    public abstract IList<IEntities> SelectAll();
}

public class Server : Entity
{
    public IList<Entity> SelectAll()
    {
        return new IList<Server>();   // Compile Error 
    }
}

Aucun commentaire:

Enregistrer un commentaire