猜谜语:为什么会出现隐式接口实现错误?

发布于 2024-12-12 05:01:14 字数 1381 浏览 0 评论 0原文

考虑以下代码行:

public interface IProduct
{
    string Name { get; set; }
}

public interface IProductList
{
    string Name { get; }

    IProduct GetValueObject();
}

public abstract class BaseProductList<T> : IProductList where T : class, IProduct, new()
{
    public abstract T GetValueObject();

    public string Name { get; set; }
}

这给了我以下警告: 错误 1 ​​ConsoleApplication1.EnumTest.BaseProductList-T- 未实现接口成员 ConsoleApplication1.EnumTest.IProductList.GetValueObject()。 ConsoleApplication1.EnumTest .BaseProductList-T-.GetValueObject() 无法实现 ConsoleApplication1.EnumTest.IProductList.GetValueObject(),因为它没有匹配的返回类型ConsoleApplication1.EnumTest.IProduct

<子> (错误 1 ​​'ConsoleApplication1.EnumTest.BaseProductList' 不 实现接口成员 'ConsoleApplication1.EnumTest.IProductList.GetValueObject()'。 'ConsoleApplication1.EnumTest.BaseProductList.GetValueObject()' 无法实施 'ConsoleApplication1.EnumTest.IProductList.GetValueObject()' 因为 它没有匹配的返回类型 'ConsoleApplication1.EnumTest.IProduct'。 \cencibel\homes$\k.bakker\视觉 工作室 2010\Projects\ConsoleApplication1\ConsoleApplication1\EnumTest\Program.cs 29 23 TestApp)

但是当我添加这段明确的代码时,它就可以工作了:

IProduct IProductList.GetValueObject()
{
    return GetValueObject();
}

为什么 .Net 无法弄清楚这一点!?

Consider the following lines of code:

public interface IProduct
{
    string Name { get; set; }
}

public interface IProductList
{
    string Name { get; }

    IProduct GetValueObject();
}

public abstract class BaseProductList<T> : IProductList where T : class, IProduct, new()
{
    public abstract T GetValueObject();

    public string Name { get; set; }
}

This gives me the following warning:
Error 1 ConsoleApplication1.EnumTest.BaseProductList-T- does not implement interface member ConsoleApplication1.EnumTest.IProductList.GetValueObject(). ConsoleApplication1.EnumTest.BaseProductList-T-.GetValueObject() cannot implement ConsoleApplication1.EnumTest.IProductList.GetValueObject() because it does not have the matching return type of ConsoleApplication1.EnumTest.IProduct


(Error 1 'ConsoleApplication1.EnumTest.BaseProductList' does not
implement interface member
'ConsoleApplication1.EnumTest.IProductList.GetValueObject()'.
'ConsoleApplication1.EnumTest.BaseProductList.GetValueObject()'
cannot implement
'ConsoleApplication1.EnumTest.IProductList.GetValueObject()' because
it does not have the matching return type of
'ConsoleApplication1.EnumTest.IProduct'. \cencibel\homes$\k.bakker\visual
studio
2010\Projects\ConsoleApplication1\ConsoleApplication1\EnumTest\Program.cs 29 23 TestApp)

But when I add this explicit piece of code, it works:

IProduct IProductList.GetValueObject()
{
    return GetValueObject();
}

Why can't .Net figure this one out!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2024-12-19 05:01:14

返回 IProduct 的方法与返回 some-type-implementing-IProduct 的方法相同。您正在尝试使用协变返回类型 - .NET 不支持不支持。

基本上它类似于这种情况:

// Doesn't compile
class Foo : ICloneable
{
    public Foo Clone()
    {
        return new Foo();
    }
}

看起来不错,并允许客户端调用 Clone() 并返回强类型值 - 但它没有实现该接口。 .NET 不支持这一点,而且从来没有支持过 - 代码中的泛型只是同一问题的另一个示例。

A method returning IProduct is not the same as a method returning some-type-implementing-IProduct. You're trying to use covariant return types - which .NET doesn't support.

Basically it's similar to this situation:

// Doesn't compile
class Foo : ICloneable
{
    public Foo Clone()
    {
        return new Foo();
    }
}

Looks good, and allows clients to call Clone() and get back a strongly-typed value - but it doesn't implement the interface. This isn't supported in .NET, and never has been - the generics in your code are just another example of the same problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文