字典<学生类型,列表<学生>>到 IDictionary>?

发布于 2024-11-07 21:40:50 字数 585 浏览 1 评论 0 原文

请考虑以下代码:

class Student
{
}

enum StudentType
{
}

static void foo(IDictionary<StudentType, IList<Student>> students)
{   
}

static void Main(string[] args)
{
    Dictionary<StudentType, List<Student>> studentDict = 
                     new Dictionary<StudentType, List<Student>>();

    foo(studentDict);

    ...
}

有错误:

错误 CS1503:参数“1”:不能 转换自 'System.Collections.Generic.Dictionary>' 到 'System.Collections.Generic.IDictionary>'

有什么办法可以调用 foo 函数吗?

Please consider the following code:

class Student
{
}

enum StudentType
{
}

static void foo(IDictionary<StudentType, IList<Student>> students)
{   
}

static void Main(string[] args)
{
    Dictionary<StudentType, List<Student>> studentDict = 
                     new Dictionary<StudentType, List<Student>>();

    foo(studentDict);

    ...
}

There is the error:

error CS1503: Argument '1': cannot
convert from
'System.Collections.Generic.Dictionary>'
to
'System.Collections.Generic.IDictionary>'

Is there any way to call foo function?

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

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

发布评论

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

评论(3

奢欲 2024-11-14 21:40:50

您可以使用 Linq ToDictionary 方法创建一个新字典,其中值具有正确的类型:

static void Main(string[] args)
{
  Dictionary<StudentType, List<Student>> studentDict = new Dictionary<StudentType, List<Student>>();
  var dicTwo = studentDict.ToDictionary(item => item.Key, item => (IList<Student>)item.Value);
  foo(dicTwo);
}

You could use the Linq ToDictionary method to create a new dictionary where the value has the correct type:

static void Main(string[] args)
{
  Dictionary<StudentType, List<Student>> studentDict = new Dictionary<StudentType, List<Student>>();
  var dicTwo = studentDict.ToDictionary(item => item.Key, item => (IList<Student>)item.Value);
  foo(dicTwo);
}
德意的啸 2024-11-14 21:40:50

您将构建一个具有正确类型的新字典,将数据从旧字典复制到新字典中。

或者,您可以将原始字典更改为正确的类型。

不管怎样,不,你不能投射字典。

此限制的原因如下:

  1. 字典包含 Student 类型的值
  2. 您可以有许多实现 IStudent 的类型
  3. 您将强制转换字典的方法可能会尝试将另一个 IStudent 填充到字典中,即使它不是学生

You will have build a new dictionary with the right types, copying the data from the old one into the new one.

Or, you could change the original dictionary to be of the right type to begin with.

Either way, no, you can't cast the dictionary.

The reason for this limitation is as follows:

  1. The dictionary contains values of type Student
  2. You could have many types that implement IStudent
  3. The method you're giving the cast'ed dictionary to could potentially try to stuff another IStudent into the dictionary, even if it isn't Student
得不到的就毁灭 2024-11-14 21:40:50

将 StudentDict 的创建更改为:

Dictionary<StudentType, IList<Student>> studentDict = new Dictionary<StudentType, IList<Student>>();

change the creation of studentDict to be:

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