Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/DictionaryDecomposer.cs @ 1566

Last change on this file since 1566 was 1566, checked in by epitzer, 15 years ago

Format white space. (Ctrl-K, Ctrl-D) (#548)

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