开发者

inheritance from generic interface with type parameter

The code says it all:

interface a { }
interface ab : a { }
interface ac : a { }
interface igeneric<T> { }

//this is OK
class Clazz : igeneric<ab>, igeneric<ac>
{
}

//compiler error : 'Clazz2<T>' cannot implement both '<ac>' and 'igeneric<T>' because they may unify for some type parameter s开发者_StackOverflowubstitutions
class Clazz2<T> : igeneric<T>, igeneric<ac> where T : ab
{ 
}

Any ideas? Workarounds?


Yes, a few ideas:

  • You certainly cannot do what you are trying to do. Compiler has made it clear
  • You certainly should not need to do what you are trying to do. Generic is a type-less inheritance of behaviour. Implementing 2 type-full generic interfaces of the same kind does not make any sense.
  • You have not presented the original problem you are trying to solve. If you do, we might be able to help you with the design.

UPDATE

They are not of the same kind, one is g and the other is g. think about IMessageHandler and IMessageHandler, in a class implementing both Handle(MessageA) and Handle(MessageB)

In the real world, you would not do that. Also it can lead to numerous problems. Let's say we have:

interface IFactory<T>
{
   T Create();
}

Then if I implement two:

class DontDoIt : IFactory<A>, IFactory<B>
{
    A Create();
    B Create(); // won't compile
}

This will not compile since the methods are different only in return type. Now you can perhaps overcome it by implementing the interfaces explicitly, but as I said it just does not make sense from the design principles.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜