forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryExtender.cs
More file actions
55 lines (51 loc) · 1.86 KB
/
DictionaryExtender.cs
File metadata and controls
55 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
namespace UnitTests
{
public static class DictionaryExtender
{
public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
{
var expando = new ExpandoObject();
var expandoDic = (IDictionary<string, object>) expando;
foreach (var kvp in dictionary)
{
var innerDictionary = kvp.Value as IDictionary<string, object>;
if (innerDictionary != null)
{
var expandoValue = innerDictionary.ToExpando();
expandoDic.Add(kvp.Key, expandoValue);
}
else
{
var innerCollection = kvp.Value as ICollection;
if (innerCollection != null)
{
var itemList = new List<object>();
foreach (var item in innerCollection)
{
var nestedDictionary = item as IDictionary<string, object>;
if (nestedDictionary != null)
{
var expandoItem = nestedDictionary.ToExpando();
itemList.Add(expandoItem);
}
else
{
itemList.Add(item);
}
}
expandoDic.Add(kvp.Key, itemList);
}
else
{
expandoDic.Add(kvp);
}
}
}
return expando;
}
}
}