【发布时间】:2011-02-09 10:56:44
【问题描述】:
这是我的整个应用程序。对我来说,这应该编译得很好。
namespace SampleApp
{
interface I<T> where T : Image
{
}
class A<T> where T : Image
{
}
class B : A<Bitmap>, I<Bitmap>
{
}
class C : A<Metafile>, I<Metafile>
{
}
class Program
{
public static I<Image> SomeProperty { get; set; }
static void Main(string[] args)
{
B b = new B();
SomeProperty = b;
C c = new C();
SomeProperty = c;
}
}
}
但是,“SomeProperty = b”和“SomeProperty = c”行给出了错误:
Cannot implicitly convert type 'B' to 'I<System.Drawing.Image>'. An explicit conversion exists (are you missing a cast?)
但我不知道为什么。 B 实现了I<Bitmap>,而Bitmap 是Image 的子类,所以根据定义,B 肯定实现了I<Image>。
【问题讨论】:
-
您使用的是哪个版本的 .NET Framework?
-
.NET 4,很抱歉漏掉了。
标签: c# .net interface generics