I have the following object architecture :
public interface IConfigAlarmBase{}
public interface IConfigAlarmBinary:IConfigAlarmBase{}
public interface IConfigAlarmRanged:IConfigAlarmBase{}
public interface IAlarmItem<TConfig> where TConfig : IConfigAlarmBase{}
public abstract class AlarmItemBase<TConfig> : IAlarmItem<TConfig> where TConfig :IConfigAlarmBase {}
public abstract class AlarmItemBinaryBase:AlarmItemBase<IConfigAlarmBinary>{}
public abstract class AlarmItemRangedBase<TConfig> : AlarmItemBase<TConfig> where TConfig:IConfigAlarmRanged{}
public class AlarmImplementation:AlarmItemRangedBase<IConfigAlarmRanged>
And then there are some implementations of the AlarmItemBinaryBase
object and its brother the AlarmItemRangedBase
.
I wanted to store all my alarms in a single list that would be defined as : List<IAlarmItem<IConfigAlarmBase>>
But when I instantiate my list and try to fill it I get an error:
System.InvalidCastException: Unable to cast object of type 'Alarm.Implementations.AlarmImplementation' to type 'Common.Application.Alarm.IAlarmItem`1[Common.Config.Alarms.IConfigAlarmBase]'.
Which I don't understand as the AlarmImplementation
is an implementation of IAlarmItem
(through the class hierarchy of the AlarmItemRangedBase
) and the IConfigAlarmRanged
is a child of the IConfigAlarmBase
. To me this was supposed to be a downcast as all the classes are derived from the Base one.
But it seems I'm wrong. Would it be possible that when the compiler replace the templates it then consider the generic type as specific type ignoring the relationships between the templated types ?
Thanks for all the consideration you'll give to this question.