2012-01-10 96 views
-5

我使用第三方库,我未能够获得对象数组内容访问对象为字典

IDictionary dic=SomeFunc(); // this function returns an IDictionary 

和我使用的DictionaryEntry来获得它的内容

foreach(DictionaryEntry de in dic) 
{ 
    //each of de.Value is implemented as a KeyValuePair<object,object> 
    //I have not yet learned how to read each de.Value's key and value pair 
} 

你能不能给我提示获取de.Value中的字符串,这是一个对象的字典对象?

编辑 de.Values的每个值都是“object”到“object”类型的键值对。它会报告错误,当我将它转换为字符串

foreach (DictionaryEntry de in dic) 
{ 
    foreach(KeyValuePair<string,string> k in (Dictionary<string,string>)de.Values) 
    { 
     //error: instance is null 
    } 
} 
+0

不要忘记为接受的标记答案,如果你有信息想要 – 2012-01-18 07:00:34

回答

0

化妆用的是keywork

if(de.value is typeof(Dictionary)) 
//than do code 

不知道,但这个会工作

0

只能访问值,有这样的:

foreach (object value in dic.Values) 
{ 
    MessageBox.Show(value.ToString()); 
} 

如果你的意思是每个值都是Dictionary,那么你可以嵌套循环:

foreach (object value in dic.Values) 
{ 
    Dictionary<object, object> nestedDic = (Dictionary<object, object>)value; 
    foreach (object nestedValue in nestedDic.Values) 
    { 
     MessageBox.Show(nestedValue.ToString()); 
    } 
} 
+0

谢谢,外for循环报告错误的对象不具有公共GetEnumerator方法, – 2012-01-10 11:10:08

+0

什么确切的行导致该错误? – 2012-01-10 11:16:45