0

I do not know if I understood something wrong. But Visual Studio says that adding an item does not allow the conversion from ExporterTaskWorker<ExporterTypeMusic> to ExporterTaskWorker<IExporterType>. But ExporterTypeMusic implements the IExporterType interface. What am I doing wrong?

public interface IExporterType
{
    bool BrandChannelAssigning();
}

public class ExporterTypeMusic : IExporterType
{
    public bool BrandChannelAssigning()
    {
        throw new System.NotImplementedException();
    }
}

public class ExporterTaskWorker<T> : INotifyPropertyChanged where T : IExporterType
{
    public Config TheConfig { get; set; }

    public object SomeProperty { get; set; }
    ...
    ...

    public ExporterTaskWorker(Config _config) {

    }
}



public class SomeClass
{
    public ObservableCollection<ExporterTaskWorker<IExporterType>> ExporterInstanceCollection { get; set; } = new ObservableCollection<ExporterTaskWorker<IExporterType>>();

    public void SomeMethod()
    {
        Config theConfig = new Config();
        ExporterTaskWorker<ExporterTypeMusic> exporterTaskWorker = new ExporterTaskWorker<ExporterTypeMusic>(theConfig);

        ExporterInstanceCollection.Add(exporterTaskWorker);

    }
}
Marcus
  • 654
  • 1
  • 8
  • 19
  • Why not do something like this? `ExporterTaskWorker exporterTaskWorker = new ExporterTaskWorker();` – Jan Paolo Go Jun 15 '18 at 00:41
  • This behaviour is know as contravariance. The accepted answer explains why what you are trying to do is not allowed with Generics. (see link below) – Nik Jun 15 '18 at 00:54
  • Possible duplicate of [Difference between Covariance & Contra-variance](https://stackoverflow.com/questions/2184551/difference-between-covariance-contra-variance) – Nik Jun 15 '18 at 00:56
  • @Nik - That's not a duplicate. It's certainly related to this question, but an answer to the linked question would not be able to be dropped in here as an answer. – Enigmativity Jun 15 '18 at 01:05
  • @Marcus - Just because `ExporterTypeMusic` is-a `IExporterType` it does not mean that `ExporterTaskWorker` is-a `ExporterTaskWorker`. It is not. – Enigmativity Jun 15 '18 at 01:06
  • Ok. Strange thing – Marcus Jun 15 '18 at 01:22

0 Answers0