转换通用字典<>到 ICollection<>问题

发布于 2024-08-13 07:08:56 字数 496 浏览 5 评论 0 原文

下面是这种情况的一个示例:

public class ScheduleArea : IArea<Schedule>
{
//....
private Dictionary<int, ScheduleArea> subArea;
//.... 

#region IArea<Schedule> Members

public ICollection<KeyValuePair<int, IArea<Schedule>>> SubArea
{
     get {
        return (Collection<KeyValuePair<int, IArea<Schedule>>>)this.subArea;//Error here
     }

}

#endregion

subArea 包含一个 ScheduleArea,它实际上是一个 IArea。为什么转换不起作用以及如何修复它?

Here is an exemple of the situation:

public class ScheduleArea : IArea<Schedule>
{
//....
private Dictionary<int, ScheduleArea> subArea;
//.... 

#region IArea<Schedule> Members

public ICollection<KeyValuePair<int, IArea<Schedule>>> SubArea
{
     get {
        return (Collection<KeyValuePair<int, IArea<Schedule>>>)this.subArea;//Error here
     }

}

#endregion

subArea is containing a ScheduleArea that is in fact an IArea. Why does the conversion doesnt work and how can I fix it?

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

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

发布评论

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

评论(3

绿光 2024-08-20 07:08:56

它不起作用,因为您假设通用方差,而在 .NET 4.0 之前它根本不起作用。

最简单的方法是手动完成(考虑到“c#2.0”标签,我假设您不能使用 LINQ):

List<KeyValuePair<int, IArea<Schedule>>> list = new
    List<KeyValuePair<int, IArea<Schedule>>>(subArea.Count);
foreach (KeyValuePair<int, ScheduleArea> pair in subArea)
{
    list.Add(new KeyValuePair<int, IArea<Schedule>>(pair.Key, pair.Value);
}
return list;

考虑到正在进行的复制量,我会将其设为方法而不是属性。

It doesn't work because you're assuming generic variance, which doesn't work at all until .NET 4.0.

The simplest way forward is going to be to do it manually (given the "c#2.0" tag I assume you can't use LINQ):

List<KeyValuePair<int, IArea<Schedule>>> list = new
    List<KeyValuePair<int, IArea<Schedule>>>(subArea.Count);
foreach (KeyValuePair<int, ScheduleArea> pair in subArea)
{
    list.Add(new KeyValuePair<int, IArea<Schedule>>(pair.Key, pair.Value);
}
return list;

I would make this a method rather than a property though, given the amount of copying going on.

自由如风 2024-08-20 07:08:56

您遇到了非常流行的通用协方差/逆变问题。您基本上是在尝试将 Dictionary 转换为 Dictionary>。 .NET 中的泛型不能像这样分配,

但是,您可以将其转换为 IDictionaryICollection 类型。 >。要实际获取 ICollection>,您需要更改 subArea 变量类型或创建一个新字典:

Dictionary<int, IArea<Schedule>> dict = new Dictionary<int, IArea<Schedule>>(subArea.Count);

foreach (KeyValuePair<int, ScheduleArea> kvp in subArea) 
{
    dict.Add(kvp.Key, kvp.Value);
}

return dict;

此外, Dictionary 不会继承 Collection - 您需要使用 ICollection 代替

You're running into the very popular problem of generic co/contra-variance. You're basically trying to cast a Dictionary<int, ScheduleArea> to a Dictionary<int, IArea<Schedule>>. Generics in .NET aren't assignable like that

However, you could cast it to an IDictionary<int, ScheduleArea> or ICollection<KeyValuePair<int, ScheduleArea>>. To actually get an ICollection<KeyValuePair<int, IArea<Schedule>> you need to either change the subArea variable type or create a new dictionary:

Dictionary<int, IArea<Schedule>> dict = new Dictionary<int, IArea<Schedule>>(subArea.Count);

foreach (KeyValuePair<int, ScheduleArea> kvp in subArea) 
{
    dict.Add(kvp.Key, kvp.Value);
}

return dict;

Also, Dictionary doesn't inherit off Collection - you need to use ICollection instead

私野 2024-08-20 07:08:56

啊,关于协方差的日常问题。

请参阅:为什么可以我是否通过了 List作为接受 List?

转换列表 List

总结一下:每个项目都必须转换为新集合的新 KeyValuePair。

Ah, the daily question about covariance.

See: Why can't I pass List<Customer> as a parameter to a method that accepts List<object>?

and

Convert List<DerivedClass> to List<BaseClass>

To sum up: each item will have to be converted into a new KeyValuePair for the new collection.

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