如何从对象{System.Collections.Generic.KeyValuePair <string,object =“”>中提取值?

时间:2019-02-13 13:44:36

标签: c# object

我有以下对象:

object {System.Collections.Generic.KeyValuePair<string, object>}

键是“ ipAddresses”字符串 值为object[],其中此数组中的[0]是一个对象[字符串] 我想摘录。 (这是一个IP地址)

除其他外,我尝试了以下代码:

object.GetType().Getproperty("ipAddresses")
object.GetType().GetField("ipAddresses")

请注意,对象类型为:(来自观察)

{[ipaddresses,System.Object []]}

类型

object {System.Collections.Generic.KeyValuePair}

我想将value[0]从KeyValuePair保存到字符串。任何帮助将不胜感激:)

2 个答案:

答案 0 :(得分:2)

object obj; //get key value pair from somewhere
object ips = ((KeyValuePair<string, object>)obj).Value;
string ip = ((object[])ips)[0].ToString();

答案 1 :(得分:1)

string ipAddress = object["ipAddresses"][0];
相关问题