C#泛型类中的协方差 [英] Covariance in C# generic class
问题描述
C# 4.0 .NET 4.5 Silverlight 5我找不到解决方案似乎很奇怪,所以需要一些帮助.
C# 4.0 .NET 4.5 Silverlight 5 It seems weird that I cant find the solution so need some help please.
我有基类 Base 和派生类 Child : Base.我还有辅助类,它具有通用类型来执行特定工作,一个 EF 实体 Helper where T : EntityObject.
I have base class Base and derived class Child : Base. I have also helper class which has generic type to do specific work one EF entities Helper where T : EntityObject.
Child 使用特定实体 MyEntity : EntityObject 执行特定工作.
Child does specific work with a specific entity MyEntity : EntityObject.
所以我尝试了:
public class Base
{
protected Helper<EntityObject> helper;
}
public class Child : Base
{
public Child()
{
helper = new Helper<MyEntity>();
}
}
我希望更多的派生类必须知道更具体的泛型参数,我认为这就是协方差...但这不起作用...
I would expect that more derived class must know about more specific generic parameter and I think that's what covariance for... But that doesn't work...
设计这样的课程的正确"方法是什么?
What's the 'correct' way to design class like that?
抱歉,我没有 100% 清楚为什么我不能实现我需要的.
sorry I didn't make it 100% clear why cant I achieve what I need.
一个.使用通用 Base 的解决方案不起作用,因为 Base 的用户不知道 T 类型.想象一下:
a. Solution with generic Base doesn't work because user of the Base doesn't know the T type. Imagine:
public class User
{
private Base<T> base; // this will not compile.
public User(TypeEnum t)
{
if(t == TypeEnum.MyEntity) base = new Child();
...
b.带有接口的解决方案 不起作用,因为助手在任何地方都使用 T(这是它的目的吗?).想象它有方法
b. Solution with Interface doesn't work because helper uses T everywhere (it is its purpose right?). Imagine it has method
public IEnumerable<T> Process(IEnumerable<T> items) { return items; }
如何在对T一无所知的界面中调出它
How do I bring it up in the interface which doesn't know anything about T
推荐答案
如果 Foo
: Bar
,那并不意味着 Some
: Some
.有两种方法可以做你想做的事.第一个是使基类型泛型如下:
If Foo
: Bar
, that doesn't mean that Some<Foo>
: Some<Bar>
. There are two ways of doing what you want. The first is to make the base-type generic such that:
Base<T> where T : EntityObject {
protected Helper<T> helper;
}
Child : Base<MyEntity> {...}
第二种是在基类型上使用非泛型接口,即有
The second is to use a non-generic interface at the base-type, i.e. have
Base {
protected IHelper helper;
}
Child : Base {...}
在后一种情况下,Helper
,用于一些非泛型的IHelper
待定义.
where in the latter case, Helper<T> : IHelper
, for some non-generic IHelper
to-be-defined.
作为旁注,您可能发现在构造函数中向下传递值比使用 protected
字段更容易.
As a side-note, you might find it easier to pass the value down in the constructor rather than using a protected
field.
这篇关于C#泛型类中的协方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!