我正在尝试创建特定类的类型,但我无法将它们作为其通用表示返回,有人能告诉我如何实现吗?我对逆变/协变魔术有些陌生。
public DashboardNotification<IDashboardEntry> Get()
{
//return new MyNotWorkingNotification(); // doesn't compile, I want to achieve this
return new MyWorkingNotification(); // compiles
}
public class DashboardNotification<T> where T : IDashboardEntry
{
}
public interface IDashboardEntry
{
}
public class MyNotWorkingNotification : DashboardNotification<MyDashboardEntry>
{
}
public class MyWorkingNotification : DashboardNotification<IDashboardEntry>
{
}
public class MyDashboardEntry : IDashboardEntry
{
}