Ethan Allen
Ethan Allen

Reputation: 14835

How do I cast this type into a Dictionary?

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.

Upvotes: 0

Views: 2918

Answers (2)

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

Try using this

Dictionary<string, IPropertyListDictionary> maindict = (data["section0"].DictionaryItems).ToDictionary(x => x.Key, x => x.Value);

Upvotes: 4

devlord
devlord

Reputation: 4164

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();

Upvotes: -2

Related Questions