Itamar Levy
Itamar Levy

Reputation: 93

How do I extract value from object {System.Collections.Generic.KeyValuePair<string, object>?

I have the following object:

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

Key is "ipAddresses" string Value is object[] where [0] in this array is an object[string] that I would like to extract. (it's an IP address)

I tried the following code among other things with no success:

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

Note that the object type is: (from watch)

value

{[ipAddresses,System.Object[]]}

Type

object{System.Collections.Generic.KeyValuePair}

I'd like to save value[0] from the KeyValuePair to a string. Any help would be highly appreciated :)

Upvotes: 2

Views: 3316

Answers (2)

Alexander
Alexander

Reputation: 9642

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

Upvotes: 2

spodger
spodger

Reputation: 1679

string ipAddress = object["ipAddresses"][0];

Upvotes: 1

Related Questions