Suppose we have this model:
public abstract class AbstractTableReferentielEntity {} public class EstimationTauxReussite : AbstractTableReferentielEntity { }
I created an extension method for all classes that inherit from AbstractTableReferentielEntity.
public static EntityItemViewModel ToEntityItem<T>(this T entity) where T : AbstractTableReferentielEntity {}
But for one specific type of AbstractTableReferentielEntity (e.g. EstimationTauxReussite) I would like to perform a specific action, so I created a second extension method.
public static EntityItemViewModel ToEntityItem(this EstimationTauxReussite entity) {}
All extension methods are declared in the same namespace.
After that, I retrieve some data from the database using the Entity Framework:
protected List<EntityItemViewModel> GetAllActifEntityItem<T>() where T : AbstractTableReferentielEntity { return Context .Set<T>() .Where(item => item.IsActif) .Select(item => item.ToEntityItem()) .ToList(); }
It compiles.
When T is an EstimationTauxReussite type at runtime, it goes into the wrong ToEntityItem method when I call Select(item => item.ToEntityItem()) . It is not included in the most specific extension method. Any ideas?
source share