mardi 23 décembre 2014

Retrieving all types that implement an interface result differ between Covariant and Coinvariant interfaces

I'm trying to get all types that implement a certain interface but it doesn't work when the Interface is co-invariant.


This is what I'm trying:



public class BaseWidgetViewModel {}
public class Widget {}

public interface IWidgetUpdated<in TViewModel> where TViewModel : BaseWidgetViewModel, new()
{
Widget UpdateFromViewModel(BaseWidgetViewModel viewModel);
}

public abstract class TestBaseCcWidget<TViewModel> : IWidgetUpdated<TViewModel> where TViewModel : BaseWidgetViewModel, new()
{
public abstract Widget UpdateFromViewModel(BaseWidgetViewModel viewModel);
}

void Main()
{
Console.WriteLine(
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => typeof(IWidgetUpdated<BaseWidgetViewModel>).IsAssignableFrom(p)).ToList().Count()
);

}


This will return 0 but when I change the interface to IWidgetUpdated<out TViewModel> then I get a result of 2.


What am I doing wrong?






Aucun commentaire:

Enregistrer un commentaire