在发布这篇文章之前,我已经尽可能多地搜索和阅读/研究了似乎合理的内容。我发现了类似的问题,但大多数帖子实际上更多地涉及将“派生类型列表”传递给需要“基本类型列表”的函数调用。我可以欣赏动物的例子,并且感觉在学习之后我有了更好的理解。
话虽如此,我仍然无法弄清楚如何解决我的特定用例。我需要在集合中聚合 “GenericClass of TestInterface(s)” 的实例。我已经在下面复制/粘贴了我最大的努力,这似乎是完成任务的最佳方式。
namespace Covariance
{
class Program
{
protected static ISet<GenericClass<TestInterface>> set = new HashSet<GenericClass<TestInterface>>();
static void Main(string[] args)
{
set.Add(new GenericClass<A>());
set.Add(new GenericClass<B>());
}
}
class GenericClass<TemplateClass> where TemplateClass : TestInterface
{
TemplateClass goo;
}
public interface TestInterface
{
void test();
}
public class A : TestInterface
{
public void test()
{
}
}
class B : A
{
}
}
以上代码因以下编译错误而失败:
error CS1503: Argument 1: cannot convert from 'Covariance.GenericClass' to 'Covariance.GenericClass'
error CS1503: Argument 1: cannot convert from 'Covariance.GenericClass' to 'Covariance.GenericClass'
任何帮助/指导或相关链接将不胜感激。如果这是一个重复的问题,我再次表示歉意。谢谢!
最佳答案
您只能在泛型接口(interface)上声明变体修饰符(in、out),而不能在类型上声明。因此,解决此问题的一种方法是为您的 GenericClass
声明接口(interface),如下所示:
interface IGenericClass<out TemplateClass> where TemplateClass : TestInterface {
TemplateClass goo { get; }
}
class GenericClass<TemplateClass> : IGenericClass<TemplateClass> where TemplateClass : TestInterface
{
public TemplateClass goo { get; }
}
然后
class Program {
protected static ISet<IGenericClass<TestInterface>> set = new HashSet<IGenericClass<TestInterface>>();
static void Main(string[] args) {
set.Add(new GenericClass<A>());
set.Add(new GenericClass<B>());
}
}
关于c# - 我已经阅读了所有关于协变、逆变和不变的内容,但我仍然不知道如何设计我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40758996/