0

I have the following code. It uses a class someone built to manage a specific file type called a plist. I am trying to take what it spits out to me and put it in a Dictionary.

Dictionary<string, IPropertyListDictionary> maindict = new Dictionary<string, IPropertyListDictionary>();
maindict = data["section0"].DictionaryItems;

The problem is that I get a red line under "DictionaryItems" with the following error:

Cannot implicitly convert type
 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,CodeTitans.Core.Generics.IPropertyListItem>>' 
to
 'System.Collections.Generic.Dictionary<string,CodeTitans.Core.Generics.IPropertyListDictionary>'. 
An explicit conversion exists (are you missing a cast?)

Can anyone help me correctly cast this? Thanks.

Marlon
  • 19,924
  • 12
  • 70
  • 101
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194

2 Answers2

4

Try using this

Dictionary<string, IPropertyListDictionary> maindict = (data["section0"].DictionaryItems).ToDictionary(x => x.Key, x => x.Value);
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
  • 4
    You are creating a new dictionary instance in your first line for no reason at all. It should be just `Dictionary maindict = (data["section0"].DictionaryItems).ToDictionary(x => x.Key, x => x.Value); ` – Adi Lester Feb 08 '12 at 23:11
-2

You can't cast it, but you can convert it by writing your own loop or using the ToDictionary() extension method in Linq.

using System.Collections.Generic;
using System.Linq;

// ...
Dictionary<string, IPropertyListDictionary> mainDictionary = data["section0"].DictionaryItems.ToDictionary();
devlord
  • 4,054
  • 4
  • 37
  • 55