0

i have following code :

private List<DataPackageHandler<ISourceDataEntity<T, TD>, IValueObject<TD>, T, TD>> Handlers { get; }

and i try to add following :

public void CreateNewDataPackageHandler<TI, TO>(IMapper<TI, TO, T, TD> mapping, IDataService<TO, TD> dataService, TD id)
            where TI : ISourceDataEntity<T, TD> 
            where TO : IValueObject<TD>
        {
            var handler = DataPackageHandler<TI, TO, T, TD>.CreateNewDataPackageHandler(mapping, dataService, id) as DataPackageHandler<ISourceDataEntity<T, TD>, IValueObject<TD>, T, TD>;
            Handlers.Add(handler);
        }

but var handler is null

what am i doing wrong ?

thanks a lot for help

UPDATE

i created a

public abstract class AHandler<T, TD>

so my List is now defined as

private List<AHandler<T, TD>> Handlers { get; }

which is capable of holding also a object of type

DataPackageHandler<TI, TO, T, TD>

thanks a lot for help !

  • Just because `TO : IValueObject` does not mean that `G : G>`, so the cast fails and you get `null` back. – D Stanley Feb 12 '16 at 22:19
  • ok thanks a lot, can i somehow wrap it up ? to put it in a list – Karsten Mikulcak Feb 12 '16 at 22:55
  • per the marked duplicate, and many similar questions already on Stack Overflow, C# does not allow this unsafe use of generic variance. Because it's unsafe. Depending on the specifics of your situation, you may be able to change your declarations so that the variance is legal, but there's not enough information in your question to understand those specifics. If you feel your operation should be safe, please research the topic first. If after doing so you still need help, post a new question in which you've provided a good [mcve] showing what you've tried, with a clear, specific question. – Peter Duniho Feb 12 '16 at 23:31

0 Answers0