考虑一个类 Foo
完全实现IFoo
.
同时考虑一个任务Task<IFoo>
.
为什么调用下面的时候会抛出编译错误;
Task<IFoo> task = Task.Factory.StartNew(() => new Foo());
编译器声明它无法从源类型转换 Task<Foo>
至 Task<IFoo>
.虽然这是有道理的,因为它们本质上是两种不同的类型,但它不会属于与 IList<IFoo> list = new List<IFoo>{new Foo(/* construct */)};
相同的前提吗? ,或其他类似的作业?
目前我正在强制对界面进行强制转换,但这感觉没有必要。
最佳答案
那是因为这条语句Task.Factory.StartNew(() => new Foo());
返回 Task<Foo>
类型的实例.
鉴于Task<>
class 是一个具体的类,它不能是协变的,除非它实现了一个协变接口(interface)(即 ITask<out T>
)。
请注意,有一个 uservoice 主题可以做到这一点:"Make Task implement covariant interface ITask" .
另请注意以下可能的解释,说明为什么现在是这样,"Lack of Covariance in the Task Class" :
Framework guidelines are:
- If your framework already includes an interface for other reasons, then by all means make it co+contravariant.
- But don't introduce an interface solely for the purposes of enabling co+contravariance.
The justification is that the advantage of covariance is outweighed by the disadvantage of clutter (i.e. everyone would have to make a decision about whether to use
Task<T>
orITask<T>
in every single place in their code).
现在你必须做:
Task<IFoo> task = Task.Factory.StartNew<IFoo>(() => new Foo());
关于c# - 将具体的 Task<TImplementation> 分配给 Task<TInterface> 类型的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430267/