Foreach对象循环

时间:2013-05-24 14:31:35

标签: c# loops object foreach

我已经编码:

foreach (KeyValuePair<string, object> keyValuePair in items)
{
    if (keyValuePair.Key == "animals")
    {
        Dictionary<string, object> animal = new Dictionary<string, object>();
        animal.Add(keyValuePair.Key, keyValuePair.Value);
        object animalObject = keyValuePair.Value;
        foreach (??? in animalObject)
        {

        }
    }
    object fajny = keyValuePair.Value;

    result2 = fajny as ArrayCollection;
}

我想从animalObject对象循环Dictionary元素:

  

http://scr.hu/0tgp/6dteu   http://screenshu.com/static/uploads/temporary/1h/1p/05/3p9fyc.jpg

我不知道这是怎么回事。我无法在任何地方搜索帮助。

2 个答案:

答案 0 :(得分:1)

您是否正在尝试遍历字典中的值:animal

    foreach (object animalObject in animal.Values)
    {

    }

答案 1 :(得分:0)

您可以尝试转换为IEnumerable,但我建议您尝试保留强力输入。

foreach (KeyValuePair<string, object> keyValuePair in items)
{
    if (keyValuePair.Key == "animals")
    {
        Dictionary<string, object> animal = new Dictionary<string, object>();
        animal.Add(keyValuePair.Key, keyValuePair.Value);
        IEnumerable animalObject = keyValuePair.Value as IEnumerable;

        if(animalEnumerable != null)
        {
            foreach (object animalObj in animalEnumerable)
            {

            }
        }
    }
}