c# - 如何从对象 {System.Collections.Generic.KeyValuePair<string, object> 中提取值?

标签 c# object

我有以下对象:

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

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

我尝试了以下代码,但没有成功:

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

请注意,对象类型是:(来自 watch )

{[ipAddresses,System.Object[]]}

类型

对象{System.Collections.Generic.KeyValuePair}

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

最佳答案

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

关于c# - 如何从对象 {System.Collections.Generic.KeyValuePair<string, object> 中提取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671662/

相关文章:

c# - 使用 nameof() 与其父类一起检查类名以进行 MVC 验证

javascript - 对数组中的第一个和最后一个值进行分组(连续值与第一个和最后一个元素分组)

object - 如何获取特定 cdi session 范围 bean 的所有实例

c# - 有没有办法强制将所有引用的程序集加载到应用程序域中?

c# - 使用内部构造函数创建泛型类

c# - 无法在 klov reporter 中保存屏幕截图。 (范围报告)

c# - MVVM Light 中有两种 ViewModel 吗?

带有对象键的 JavaScript setter 和 getter

javascript - 检查 Object -> key 是否已经在数组中

javascript - 如何删除包含相同 `place` 的重复 `name` 名称?