Home > front end >  Inheritance problems when using T types in base forms
Inheritance problems when using T types in base forms

Time:01-10

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'. There is no implicit reference conversion from 'AddressType.AddressTypeForm_List' to 'Models.Base.CoreBaseEntity>'.

Thanks for the help.

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

CodePudding user response:

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();
  • Related