使用基本形式中的T类型时的继承问题

huangapple go评论63阅读模式
英文:

Inheritance problems when using T types in base forms

问题

public partial class BaseListForm : RibbonForm where TEntity : CoreBaseEntity {}

我将TEntity传递给了我的BaseListForm,并且来自CoreBaseEntity的谓词,

public partial class AddressTypeForm_List : BaseListForm<Si_AddressType> // Si_AddressType继承自CoreBaseEntity {}

其他继承自BaseListForm的表单看起来像这样,

public class ShowListForms where TForm : BaseListForm {}

我有一个名为ShowListForms的类,我用它来特定目的,它也继承自BaseListForm。

ShowListForms<AddressTypeForm_List>.ShowListForm();

当我在ShowListForm中使用表单时,我遇到了这个问题。

错误:

错误 CS0311:类型 'AddressType.AddressTypeForm_List' 无法用作泛型类型或方法 'ShowListForms' 中的类型参数 'TForm'。不存在从 'AddressType.AddressTypeForm_List' 到 'Models.Base.CoreBaseEntity>' 的隐式引用转换。

感谢帮助。

如果我在第二段代码部分中使用 BaseListForm 而不是 BaseListForm<Si_AddressType>,问题就会解决,但我需要一个解决方法。

英文:
public partial class BaseListForm&lt;TEntity&gt; : RibbonForm where TEntity : CoreBaseEntity {}

I gave TEntity to my BaseListForm and predicate from CoreBaseEntity,

 public partial class AddressTypeForm_List : BaseListForm&lt;Si_AddressType&gt; // Si_AddressType inherits from CoreBaseEntity {}

and other forms inherits from BaseListForm looks like this,

public class ShowListForms&lt;TForm&gt; where TForm : BaseListForm&lt;CoreBaseEntity&gt; {}

i have a class called ShowListForms where i use for specific purposes,
it also inherits from BaseListForm.

 ShowListForms&lt;AddressTypeForm_List&gt;.ShowListForm();

I am having this problem when i use form in ShowListForm.

ERROR ;

Error CS0311 The type 'AddressType.AddressTypeForm_List' cannot be used as type parameter 'TForm' in the generic type or method 'ShowListForms<TForm>'. There is no implicit reference conversion from 'AddressType.AddressTypeForm_List' to 'Models.Base.CoreBaseEntity>'.

Thanks for the help.

If i use BaseListForm<CoreBaseEntity> instead of BaseListForm<Si_AddressType> in second code part the problem resolves but i need a workaround.

答案1

得分: 0

抱歉,你需要在所有子类中随处携带TEntity才能使这个工作。

问题在于public class ShowListForms&lt;TForm&gt; where TForm : BaseListForm&lt;CoreBaseEntity&gt; {}没有将任何TEntity传递给BaseListForm,因此它期望TForm是一些正好扩展了BaseListForm&lt;CoreBaseEntity&gt;的东西。

你可以这样做:

public class ShowListForms&lt;TForm, TEntity&gt; 
where TForm : BaseListForm&lt;TEntity&gt; 
where TEntity : CoreBaseEntity {}

现在当你调用它时会看起来有点复杂,但我认为它应该能工作。

ShowListForms&lt;AddressTypeForm_List,Si_AddressType&gt;.ShowListForm();
英文:

Sadly you have to carry the TEntity everywhere in the subclasses if you want this to work.

The problem is that public class ShowListForms&lt;TForm&gt; where TForm : BaseListForm&lt;CoreBaseEntity&gt; {} doesn't have any TEntity passed to BaseListForm, so it expects that TForm is something that extends exactly BaseListForm&lt;CoreBaseEntity&gt;

You can do it like this:

public class ShowListForms&lt;TForm, TEntity&gt; 
where TForm : BaseListForm&lt;TEntity&gt; 
where TEntity : CoreBaseEntity {}

Not it will look a bit more complicated when you call it but I think it should work.

 ShowListForms&lt;AddressTypeForm_List,Si_AddressType&gt;.ShowListForm();

huangapple
  • 本文由 发表于 2023年1月9日 19:29:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定