如何将子类的字典视为基类的字典 [英] How to treat a dictionary of subclasses as a dictionary of the base class

查看:116
本文介绍了如何将子类的字典视为基类的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我有一堆对象都继承自同一基类.
我也有许多词典,每个子类一个.
我想做的就是将所有这些词典添加到一个 List 中,这样我就可以遍历它们并做一些工作(例如比较列表等).

In C# I have a bunch of objects all inheriting from the same base class.
I also have a number of Dictionaries, one for each subclass.
What I want to do is add all of those Dictionaries to one List so I can loop through them all and do some work (like comparing the lists etc).

总结

Dictionary<string, Child> childObjects = new Dictionary<string, Child>();
List<Dictionary<string, Parent>> listOfDictionaries = new List<Dictionary<string, Parent>>();
listOfDictionaries.Add(childObjects);

我本以为,由于Child从Parent继承而来,所以应该可以,但是不会编译.显然,我对继承和泛型不了解:)

I would have thought that since Child inherits from Parent, this should work, but it won't compile. Clearly I am not understanding something about inheritance and generics :)

完整的代码示例

class Program
{
    static void Main(string[] args)
    {
        //Creating a Dictionary with a child object in it
        Dictionary<string, Child> childObjects = new Dictionary<string, Child>();
        var child = new Child();
        childObjects.Add(child.id, child);

        //Creating a "parent" Dictionary with a parent and a child object in it
        Dictionary<string, Parent> parentObjects = new Dictionary<string, Parent>();
        parentObjects.Add(child.id, child);
        var parent = new Parent();
        parentObjects.Add(parent.id, parent);

        //Adding both dictionaries to a general list
        List<Dictionary<string, Parent>> listOfDictionaries = new List<Dictionary<string, Parent>>();

        listOfDictionaries.Add(childObjects);  //This line won't compile

        listOfDictionaries.Add(parentObjects);


    }
}

class Parent
{
    public string id { get; set; }
    public Parent()
    {
        this.id = "1";
    }
}

class Child : Parent
{
    public Child()
    {
        this.id = "2";
    }

}

有什么办法可以做到这一点?

Is there any way of achieving this?

推荐答案

您不能安全地执行此操作.假设您这样做:

You can't do this safely. Imagine you did this:

listOfDictionaries[0]["foo"] = new Parent();

看起来 很好-但这意味着childObjects将包含一个不是Child实例的值!

That looks fine - but it would mean that childObjects would contain a value which isn't an instance of Child!

C#4在安全处引入了受限的泛型方差-例如,您可以将类型IEnumerable<Banana>的引用转换为IEnumerable<Fruit>-但是您要在此处执行的操作是'是安全的,因此仍然不允许.

C# 4 has introduced restricted generic variance where it's safe - so you can convert a reference of type IEnumerable<Banana> to IEnumerable<Fruit> for example - but what you're wanting to do here isn't safe, so it still isn't allowed.

如果您可以向我们进一步介绍更广泛的背景信息-您想要实现的目标-我们可能会提供更多帮助.之后,您能举例说明您要如何处理列表吗?

If you could tell us more about the bigger context - what you're trying to achieve - we may be able to help more. Can you give examples of what you'd want to do with the list afterwards?

这篇关于如何将子类的字典视为基类的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆