简体   繁体   English

C#中的接口继承

[英]Interfaces inheritance in C#

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> . 我有一个名为IRepository<T>根接口。 Next, IBookRepository : IRepository<Book> Next, concrete class that implements it: BookRepository : IBookRepository 接下来, IBookRepository : IRepository<Book>接下来,实现它的具体类: BookRepository : IBookRepository

In the RepositoryManager class I declared private IRepository<IRepoItem> currentRepo; 在RepositoryManager类中,我声明了private IRepository<IRepoItem> currentRepo;

IRepoItem is an interface that is implemented by Book class. IRepoItem是由Book类实现的接口。

Now, when I try to do something like this: 现在,当我尝试做这样的事情时:

currentRepo = new BookRepository();

VisualStudio gives error message: VisualStudio提供错误消息:

Cannot implicitly convert type 'BookRepository' to 'IRepository'. 无法将类型'BookRepository'隐式转换为'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. 我知道(几乎可以肯定)它是一种叫做协方差的东西,这可能(可能)在.Net 4.0中解决了。 Unfortunately I'm writing using framework version 3.5, and I cannot change this. 不幸的是我正在使用框架版本3.5编写,我无法改变它。 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. 我想从RepoFactory获得currentRepo,它会产生几种存储库,具体取决于用户的需求。 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. 我不知道这里是否允许链接,但我在http://olgatherer.wordpress.com/上写了一些博客,我在其中描述了应用程序的创建。 My English isn't good, but I hope that it's enough to understand me. 我的英语不好,但我希望能够理解我。 Thank you in advance for answer. 提前谢谢你的回答。

Best regards, skrzeczowas 最好的问候,skrzeczowas

In .NET 3.5 you definitely can't treat an IRepository<Book> as an IRepository<IRepoItem> . 在.NET 3.5中,您绝对不能将IRepository<Book>视为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? 我们需要更多地了解您在RepositoryManager使用存储库的内容,以便真正了解如何解决它...但是您是否可以创建IRepository<T>扩展的非泛型IRepository接口? Make it include all the members which don't refer to T. Then you can declare currentRepo as just an IRepository . 使它包含所有不引用T的成员。然后,您可以将currentRepo声明为IRepository

Try using the intermediate layer (IRep): 尝试使用中间层(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;

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) 在.NET 3.5中,在.NET 4中IRepository和IRepository之间没有任何关系,你可以用协方差和逆变支持来实现这样的东西(但它取决于IRepository接口声明)

A would recommand using a non generic IRepository interface and doing the cast yourself. A会建议使用非通用的IRepository接口并自己进行转换。 I know, it's not wonderful, but the fonctionality you describe requires covariance/contravariance support. 我知道,这并不好,但你所描述的功能需要协方差/逆变支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM