首页 > 解决方案 > 考虑到内部/反向/协方差,这是不可能的吗?

问题描述

我想要实现的是以下。

我有一个类似的界面

interface ISomething<T>
{
   void Input(T item);

   IEnumerable<T> Outputs();
}

和像这样的层次结构

interface IFoo { }
interface IBar : IFoo { }
interface IBaz : IFoo { }

我希望能够引用一个ISomething<IBaz>and ISomething<IBar>byISomething<IFoo>以便我可以编写类似的方法

void ProcessFoos(ISomething<IFoo> somethings)
{
    foreach (var something in somethings)
    {
       var outputs = something.Outputs();
       // do something with outputs
    }
}

其中somethings可以是ISomething<IBar>s 和ISomething<IBaz>s 的组合。

鉴于语言限制,这不可能吗?

如果没有,我该如何重新设计呢?

编辑:这是我正在谈论的一个更好的例子

public class Program
{
    public static void Main()
    {
        IBar<IX> x = new Bar<Y>() { };
        // ^^^ Cannot implicitly convert type 'Bar<Y>' to 'IBar<IX>'. An explicit conversion exists (are you missing a cast?)
    }
}

public interface IBar<T> where T : IX
{
    void In(T item);

    T Out { get; }
}

public class Bar<T> : IBar<T> where T : IX
{
    public void In(T item) { }

    public T Out { get { return default(T); } }
}

public interface IX { }

public class Y : IX { }

标签: c#.netoopinheritance

解决方案


您将somethings其视为IEnumerable,但事实并非如此。如果要遍历输出,请像这样调用它。

void ProcessFoos(ISomething<IFoo> something)
{
  foreach (var output in something.Outputs())
  {
    if(output is IBar)
    {
      // do something IBar related
    }
    else if(output is IBaz)
    {
      // do something IBaz related
    }
  }
}

如果somethings应该是IEnumerable,请更改如下签名ProcessFoos

void ProcessFoos(IEnumerable<ISomething<IFoo>> somethings)
{
  foreach (var something in somethings)
  {
    var outputs = something.Outputs();
    var barOutputs = outputs.OfType<IBar>();
    var bazOutputs = outputs.OfType<IBaz>();

    // do something with outputs
  }
}

这对我有用。

如果这对您不起作用,请提供您看到的错误和/或澄清您正在尝试但无法实现的目标。


推荐阅读