Interfaces inheritance in C#

牧云@^-^@ 提交于 2019-12-09 09:20:35

问题


I'm trying to overrun quite big (for me) problem that I came across while writing my application. Look at this, please (I will try to shorten the code for simplicity):

I have root interface called IRepository<T>. Next, IBookRepository : IRepository<Book> Next, concrete class that implements it: BookRepository : IBookRepository

In the RepositoryManager class I declared private IRepository<IRepoItem> currentRepo;

IRepoItem is an interface that is implemented by Book class.

Now, when I try to do something like this:

currentRepo = new BookRepository();

VisualStudio gives error message:

Cannot implicitly convert type 'BookRepository' to 'IRepository'. An explicit conversion exists (are you missing a cast?)

I tried to cast explicitly, but runtime exception is thrown...

I know (almost for sure) that it is something called covariance and this is (probably) solved in .Net 4.0. Unfortunately I'm writing using framework version 3.5, and I cannot change this. Please give me some advices what to do - how to overrun this problem? I'd like to get currentRepo from RepoFactory that would produce few kinds of repositories depends on user needs. I don't know whether links are allowed here, but I write some kind of blog on http://olgatherer.wordpress.com/ where I describe application creation. My English isn't good, but I hope that it's enough to understand me. Thank you in advance for answer.

Best regards, skrzeczowas


回答1:


In .NET 3.5 you definitely can't treat an IRepository<Book> as an IRepository<IRepoItem>.

We'd need to know more about what you're using the repository for in RepositoryManager to really know how to solve it... but could you create a non-generic IRepository interface which IRepository<T> extends? Make it include all the members which don't refer to T. Then you can declare currentRepo as just an IRepository.




回答2:


Try using the intermediate layer (IRep):

    interface IRepository<T>
    {
    }

    interface IRep<T> : IRepository<IRepoItem> where T : IRepoItem
    {
    }

    interface IBookRepository : IRep<Book>
    {
    }

    class BookRepository : IBookRepository
    {
    }

then you can do what you want:

        BookRepository br = new BookRepository();
        IRepository<IRepoItem> currentRepo = br;



回答3:


In .NET 3.5 there isn't any relation between IRepository and IRepository in .NET 4, you could achieve something like this with covariance and contravariance support (but it depends on the IRepository interface declaration)

A would recommand using a non generic IRepository interface and doing the cast yourself. I know, it's not wonderful, but the fonctionality you describe requires covariance/contravariance support.



来源:https://stackoverflow.com/questions/3872988/interfaces-inheritance-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!