Hi, I declared a static mainDict like this,
private static Dictionary<string, object> mainDict = new Dictionary<string, object>();
Dictionary<string, object> aSubDict = new Dictionary<string,object>();
mainDict.Add("StringA0Key", "StringValue");
mainDict.Add("StringA1Key", aSubDict);
How to detect the Value Type is a string(StringValue) or dictionary(aSubDict)
I need to detect the value type and loop into "aSubDict", otherwise i just print the "StringValue". thank you.
[updated]
Thanks for your help, i closed my question with the updatd code below.
using System;
using System.Collections;
using System.Collections.Generic;
class Program
{
private static Dictionary<string, object> mainDict = new Dictionary<string, object>();
public static void Main()
{
Dictionary<string, object> aSubDict = new Dictionary<string, object>();
mainDict.Add("sA0Key", "sA0Value");
mainDict.Add("sA1Key", aSubDict);
aSubDict.Add("sSubKey","sSubValue");
foreach (string theKey in mainDict.Keys)
{
if (!ReferenceEquals(null, mainDict[theKey]) && (mainDict[theKey] is Dictionary<string, object>))
{
//Console.WriteLine("type of aSubDict");
foreach (string subKey in aSubDict.Keys)
{
if (!ReferenceEquals(null,aSubDict[subKey]) && aSubDict[subKey] is Dictionary<string, object>)
{
Console.WriteLine("type of Child Dict");
}
else if (!ReferenceEquals(null, aSubDict[subKey]) && (aSubDict[subKey] is string))
{
Console.WriteLine("subKey = {0}, subValue = {1}", subKey, aSubDict[subKey]);
}
}
}
else if (!ReferenceEquals(null, mainDict[theKey]) && (mainDict[theKey] is string))
{
// Console.WriteLine("type string");
Console.WriteLine("Key = {0}, Value = {1}", theKey, mainDict[theKey]);
}
}
}
}