Line | |
---|
1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using HeuristicLab.Persistence.Core;
|
---|
4 | using HeuristicLab.Persistence.Interfaces;
|
---|
5 | using System.Collections.Generic;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Persistence.Default.Decomposers {
|
---|
8 |
|
---|
9 | public class DictionaryDecomposer : IDecomposer {
|
---|
10 |
|
---|
11 | public bool CanDecompose(Type type) {
|
---|
12 | return type.GetInterface(typeof(IDictionary).FullName) != null;
|
---|
13 | }
|
---|
14 |
|
---|
15 | public IEnumerable<Tag> DeCompose(object o) {
|
---|
16 | IDictionary dict = (IDictionary)o;
|
---|
17 | foreach ( DictionaryEntry entry in dict) {
|
---|
18 | yield return new Tag("key", entry.Key);
|
---|
19 | yield return new Tag("value", entry.Value);
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public object CreateInstance(Type t) {
|
---|
24 | return Activator.CreateInstance(t, true);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public object Populate(object instance, IEnumerable<Tag> o, Type t) {
|
---|
28 | IDictionary dict = (IDictionary)instance;
|
---|
29 | IEnumerator<Tag> iter = o.GetEnumerator();
|
---|
30 | while (iter.MoveNext()) {
|
---|
31 | object key = iter.Current.Value;
|
---|
32 | iter.MoveNext();
|
---|
33 | object value = iter.Current.Value;
|
---|
34 | dict.Add(key, value);
|
---|
35 | }
|
---|
36 | return dict;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.