ICollection 与 ICollection- ICollection.Count 和 ICollection.Count 之间的歧义

发布于 2024-08-06 22:55:55 字数 1443 浏览 7 评论 0原文

注意:这与另一个问题类似,但不完全相同,

我已经实现了一个IBusinessCollection 接口。它源自 ICollection 和过时的非泛型 ICollection。我宁愿转储旧的 ICollection ,但我正在使用带有 CollectionView 的 WPF 数据绑定,它希望我实现旧的非通用 IList : -(

无论如何,接口看起来像这样:

public interface IBusinessCollection<T> : ICollection<T>, ICollection
{ }

public interface ICollection<T>
{ int Count { get; } }

public interface ICollection
{ int Count { get; } }

由于使用依赖注入,我使用它们的接口传递 IBusinessCollection 类型的对象,而不是通过具体类型,所以我有这样的事情:

internal class AnonymousCollection : IBusinessCollection<string>
{ 
    public int Count { get { return 5; } }
}

public class Factory
{
    public static IBusinessCollection<string> Get()
    { return new AnonymousCollection(); }
}

当我尝试调用此代码时,出现错误,如下所示:

var counter = Factory.Get();
counter.Count; // Won't compile
// Ambiguity between 'ICollection<string>.Count' and 'ICollection.Count'

有 3 种方法可以进行编译,但它们都很丑陋

  1. 将类转换为其具体实现。 (我可能不知道)

  2. 将类显式转换为 ICollection

  3. 将类显式转换为 ICollection

是否有第四个选项根本不需要我投射东西?我可以对 IBusinessCollection 进行任何需要的更改

Note: This is similar, but not quite the same as this other question

I've implemented an IBusinessCollection interface. It dervies from both ICollection<T>, and the old-busted non-generic ICollection. I'd prefer to just dump the old busted ICollection, but I'm using WPF databinding with a CollectionView which wants me to implement the old-busted non-generic IList :-(

Anyway, the interfaces look like this:

public interface IBusinessCollection<T> : ICollection<T>, ICollection
{ }

public interface ICollection<T>
{ int Count { get; } }

public interface ICollection
{ int Count { get; } }

Due to using Dependency Injection, I'm passing around objects of type IBusinessCollection<T> using their interfaces, not by concrete types, so I have something like this:

internal class AnonymousCollection : IBusinessCollection<string>
{ 
    public int Count { get { return 5; } }
}

public class Factory
{
    public static IBusinessCollection<string> Get()
    { return new AnonymousCollection(); }
}

When I try and call this code, I get an error, as follows:

var counter = Factory.Get();
counter.Count; // Won't compile
// Ambiguity between 'ICollection<string>.Count' and 'ICollection.Count'

There are 3 ways to make this compile, but all of them are ugly.

  1. Cast the class to it's concrete implementation (which I may not know)

  2. Cast the class explicitly to ICollection

  3. Cast the class explicitly to ICollection<T>

Is there a fourth option which doesn't require me to cast things at all? I can make whatever changes I need to IBusinessCollection<T>

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

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

发布评论

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

评论(2

病毒体 2024-08-13 22:55:55

这似乎解决了我的快速测试中的问题。

public interface IBusinessCollection<T> : ICollection<T>, ICollection
{
    new int Count { get;  }
}

This appears to solve the issues in my quick tests.

public interface IBusinessCollection<T> : ICollection<T>, ICollection
{
    new int Count { get;  }
}
み零 2024-08-13 22:55:55
ICollection<string> counter = Factory.Get();
counter.Count; // No longer ambiguous
ICollection<string> counter = Factory.Get();
counter.Count; // No longer ambiguous
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文