c# - 考虑到内部/反向/协方差,这是不可能的吗?
问题描述
我想要实现的是以下。
我有一个类似的界面
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 { }
解决方案
您将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
}
}
这对我有用。
如果这对您不起作用,请提供您看到的错误和/或澄清您正在尝试但无法实现的目标。
推荐阅读
- elasticsearch - 本地无法访问 Kibana
- zoho - 如何使用自定义功能将项目名称从 Zoho Books 推送到 Zoho Creator?
- c++ - TLE 在查找岛屿数 (GFG)
- asp.net - .Net Core 中的 ModelBinder
- next.js - 从 API 读取 markdown
- php - 安装 reCaptcha v2 Checkbox 后仍然收到垃圾邮件
- swift - 使用关联类型协议作为泛型函数的返回类型
- python - 在 spreg 模型中使用分类变量
- javascript - 如何使用 javascript 对重复对象的 ID 进行分组并删除数组中的对象
- wordpress - wordpress wp_query分页不起作用