如何从泛型类型继承泛型类型

时间:2020-12-24 20:14:02

I have an abstract class:

我有一个抽象类:

public abstract class LMManager<ENTITY, ILM_ENTITY> where ENTITY : ILM_ENTITY, IActiveRecord, ICallOnCreated, new( )

ENTITY is some kind of DataObject, ILM_ENTITY, IActiveRecord, and ICallOnCreated are interfaces that the DataObject implements.

ENTITY是某种DataObject,ILM_ENTITY,IActiveRecord和ICallOnCreated是DataObject实现的接口。

Typically, I subclass this guy with classes something like

通常情况下,我将这个类子类化为类

public class JobManager : LMManager<Job, ILMJob>
public class JobViewManager : LMManager<vwJob, ILMJobView>

Now, I have a case where two of the sub-classes have some code in common, so I want to insert another layer in between, something like

现在,我有一个案例,其中两个子类有一些共同的代码,所以我想在其间插入另一个层,类似于

public abstract class JobManagerBase : LMManager<ENTITY, ILM_ENTITY>

and then change the other two guys to

然后改变其他两个人

public class JobManager : JobManagerBase<Job, ILMJob>
public class JobViewManager : JobManagerBase<vwJob, ILMJobView>

In the definition of my JobManagerBase, I get four errors related to ENTITY:

在我的JobManagerBase的定义中,我得到了与ENTITY相关的四个错误:

  1. Must be a non-abstract type with a public parameterless constructor
  2. 必须是具有公共无参数构造函数的非抽象类型

  3. No boxing conversion or type parameter conversion from ENTITY to ICallOnCreated
  4. 没有从ENTITY到ICallOnCreated的装箱转换或类型参数转换

  5. No boxing conversion or type parameter conversion from ENTITY to IActiveRecord
  6. 没有从ENTITY到IActiveRecord的装箱转换或类型参数转换

  7. No boxing conversion or type parameter conversion from ENTITY to ILM_ENTITY
  8. 没有从ENTITY到ILM_ENTITY的装箱转换或类型参数转换

Is it terribly obvious what I am missing?

我错过了什么非常明显?

1 个解决方案

#1


0  

Your JobManagerBase attempts to use LMManager with the parameters ENTITY and ILM_ENTITY.
Since these parameters do not meet your constraints, you get an error. (What if someone makes a JobManagerBase<int, string>?)

您的JobManagerBase尝试使用带有参数ENTITY和ILM_ENTITY的LMManager。由于这些参数不符合您的约束,因此会出现错误。 (如果有人创建JobManagerBase ,该怎么办?) ,string>

You need to add generic parameters and the same where clause to JobManagerBase to ensure that its parameters meet the constraints required for LMManager.

您需要向JobManagerBase添加泛型参数和相同的where子句,以确保其参数满足LMManager所需的约束。

#1


0  

Your JobManagerBase attempts to use LMManager with the parameters ENTITY and ILM_ENTITY.
Since these parameters do not meet your constraints, you get an error. (What if someone makes a JobManagerBase<int, string>?)

您的JobManagerBase尝试使用带有参数ENTITY和ILM_ENTITY的LMManager。由于这些参数不符合您的约束,因此会出现错误。 (如果有人创建JobManagerBase ,该怎么办?) ,string>

You need to add generic parameters and the same where clause to JobManagerBase to ensure that its parameters meet the constraints required for LMManager.

您需要向JobManagerBase添加泛型参数和相同的where子句,以确保其参数满足LMManager所需的约束。