Interfaces inheritance in C#

2019-03-25 16:27发布

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

3条回答
太酷不给撩
2楼-- · 2019-03-25 17:12

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-03-25 17:13

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;
查看更多
贼婆χ
4楼-- · 2019-03-25 17:30

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.

查看更多
登录 后发表回答