英文:
Inheritance problems when using T types in base forms
问题
public partial class BaseListForm
我将TEntity传递给了我的BaseListForm,并且来自CoreBaseEntity的谓词,
public partial class AddressTypeForm_List : BaseListForm<Si_AddressType> // Si_AddressType继承自CoreBaseEntity {}
其他继承自BaseListForm的表单看起来像这样,
public class ShowListForms
我有一个名为ShowListForms的类,我用它来特定目的,它也继承自BaseListForm。
ShowListForms<AddressTypeForm_List>.ShowListForm();
当我在ShowListForm中使用表单时,我遇到了这个问题。
错误:
错误 CS0311:类型 'AddressType.AddressTypeForm_List' 无法用作泛型类型或方法 'ShowListForms
感谢帮助。
如果我在第二段代码部分中使用 BaseListForm
英文:
public partial class BaseListForm<TEntity> : RibbonForm where TEntity : CoreBaseEntity {}
I gave TEntity to my BaseListForm and predicate from CoreBaseEntity,
public partial class AddressTypeForm_List : BaseListForm<Si_AddressType> // Si_AddressType inherits from CoreBaseEntity {}
and other forms inherits from BaseListForm looks like this,
public class ShowListForms<TForm> where TForm : BaseListForm<CoreBaseEntity> {}
i have a class called ShowListForms where i use for specific purposes,
it also inherits from BaseListForm.
ShowListForms<AddressTypeForm_List>.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<TForm> where TForm : BaseListForm<CoreBaseEntity> {}
没有将任何TEntity传递给BaseListForm
,因此它期望TForm是一些正好扩展了BaseListForm<CoreBaseEntity>
的东西。
你可以这样做:
public class ShowListForms<TForm, TEntity>
where TForm : BaseListForm<TEntity>
where TEntity : CoreBaseEntity {}
现在当你调用它时会看起来有点复杂,但我认为它应该能工作。
ShowListForms<AddressTypeForm_List,Si_AddressType>.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<TForm> where TForm : BaseListForm<CoreBaseEntity> {}
doesn't have any TEntity passed to BaseListForm
, so it expects that TForm is something that extends exactly BaseListForm<CoreBaseEntity>
You can do it like this:
public class ShowListForms<TForm, TEntity>
where TForm : BaseListForm<TEntity>
where TEntity : CoreBaseEntity {}
Not it will look a bit more complicated when you call it but I think it should work.
ShowListForms<AddressTypeForm_List,Si_AddressType>.ShowListForm();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论