类型参数、约束和协变/逆变
假设我有以下类,它们根据要存储的对象具有不同的实现:
public class ListOfPersistent<T> :
IList<T> where T : Persistent {... implementation ...}
public class ListOfNonPersistent<T> :
IList<T> {... implementation ...}
并且我想通过执行以下操作在上述类上使用另一个版本:
public class PersistentList<T> : IList<T> {
protected PersistentList() {
if (list != null) {
return;
}
if (Extensions.IsPersistent<T>()) {
list = new ListOfPersistent<T>();
} else {
list = new ListOfNonPersistent<T>();
}
}
protected IList<T> list;
....
}
当然,上面的内容不能编译,因为有第一个类有类型约束,第二个类没有类型约束。我有什么办法可以:告诉编译器它不应该检查这种特定情况的约束(list = new ListOfPercient
),因为我知道它将是这种类型,或者执行一些协变/逆变魔法,以便代码编译时没有任何问题?
Let's say I have the following classes that have different implementations based on the object to be stored in:
public class ListOfPersistent<T> :
IList<T> where T : Persistent {... implementation ...}
public class ListOfNonPersistent<T> :
IList<T> {... implementation ...}
And I want to use one of another version on the above classes by doing something like this:
public class PersistentList<T> : IList<T> {
protected PersistentList() {
if (list != null) {
return;
}
if (Extensions.IsPersistent<T>()) {
list = new ListOfPersistent<T>();
} else {
list = new ListOfNonPersistent<T>();
}
}
protected IList<T> list;
....
}
Of course the above does not compiles, because there is a type constrain on the first class and none on the second. Is there any way I can: Tell the compiler that it should not check the constrain on this specific case (list = new ListOfPersistent<T>()
) because I KNOW it will be of that type, or do some covariance/contravariance magic so the code compiles without any issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
协变和逆变在这里不会有帮助,因为
IList
是不变的。就我个人而言,我认为你的课程设计有缺陷。您不应该实例化
ListOfPersistent
,然后将其放入IList
类型不兼容的变量中。不幸的是,我无法提出一个好的替代方案,因为我不知道您计划如何使用这些课程或您的总体目标是什么;但我可以提出一个建议,但附带免责声明,它是hacky,并且可能只有在您真正知道自己在做什么时才应该使用:Covariance and contravariance won’t help you here because
IList<T>
is invariant.Personally I would argue that you have a flaw in your class design. You shouldn’t want to instantiate a
ListOfPersistent<T>
and then place it in a variable whose type,IList<T>
, is incompatible. Unfortunately I cannot suggest a good alternative because I have no idea how you are planning to use these classes or what your overall goal is; but I can make a suggestion with a disclaimer that it is hacky and should probably only be used if you really know what you are doing: