I'm trying to implement a factory function that accepts a base type and returns a new generic class with a derived type depending on what type the base type is, like so:
public IStaffingEventValidator<T> GetValidator<T>(T staffingEvent)
{
switch (staffingEvent)
{
case HireEvent ev:
return (IStaffingEventValidator<T>)new HireEventValidator();
default:
return null;
}
}
The problem is that type T is a base StaffingEvent
, and HireEventValidator
is of type IStaffingEventValidator<HireEvent>
, where HireEvent
is a StaffingEvent
.
I know this is not working because of covariance and contravariance, my question is how should I rather be implementing something like this? Is this a bad design? I have a list of StaffingEvents
and want to create a validator using the derived type for each of them.
Aucun commentaire:
Enregistrer un commentaire