[英]Specifying that a constrained generic class is a subclass?
I have this class definition: 我有这个类的定义:
public abstract class AbstractListViewModel<T> : AbstractWorkspaceViewModel
So I have a class called AbstractListViewModel
that has a generic type, and that is a subclass of AbstractWorkspaceViewModel
. 因此,我有一个名为
AbstractListViewModel
的类,它具有通用类型,并且是AbstractWorkspaceViewModel
的子类。
However, I need to constrain T
to only be subclasses of AbstractWorkspaceViewModel
. 但是,我需要将
T
限制为AbstractWorkspaceViewModel
子类。 I tried the following: 我尝试了以下方法:
public abstract class AbstractListViewModel<T> where T :
AbstractWorkspaceViewModel, : AbstractWorkspaceViewModel
public abstract class AbstractListViewModel<T> where T :
AbstractWorkspaceViewModel : AbstractWorkspaceViewModel
But this syntax is invalid. 但是此语法无效。
So here I am trying to say "A class called AbstractListViewModel
that is a subclass of AbstractWorkspaceViewModel
and has a generic type that is also a subclass of AbstractWorkspaceViewModel
. 所以在这里我想说的“,即A类
AbstractListViewModel
是的子类AbstractWorkspaceViewModel
并有一个通用的类型,这也是的一个子类AbstractWorkspaceViewModel
。
How do I define this? 我该如何定义?
It's the ordering of your constraint. 这是约束的顺序。 Try this:
尝试这个:
public abstract class AbstractListViewModel<T> : AbstractWorkspaceViewModel
where T : AbstractWorkspaceViewModel
You want this: 你要这个:
public abstract class AbstractListViewModel<T> : AbstractWorkspaceViewModel
where T : AbstractWorkspaceViewModel
public abstract class AbstractListViewModel<T> : AbstractWorkspaceViewModel where T : AbstractWorkspaceViewModel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.