Free cookie consent management tool by TermsFeed Policy Generator

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

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

Replace final fixes for broken parent references with separation of instance creation with meta information. (#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  public class DictionaryDecomposer : IDecomposer {
10
11    public int Priority {
12      get { return 100; }
13    }
14
15
16    public bool CanDecompose(Type type) {
17      return type.GetInterface(typeof(IDictionary).FullName) != null;       
18    }
19
20    public IEnumerable<Tag> CreateMetaInfo(object o) {
21      return new Tag[] { };
22    }
23
24    public IEnumerable<Tag> Decompose(object o) {
25      IDictionary dict = (IDictionary)o;     
26      foreach ( DictionaryEntry entry in dict) {
27        yield return new Tag("key", entry.Key);
28        yield return new Tag("value", entry.Value);
29      }
30    }
31
32    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
33      return Activator.CreateInstance(t, true);
34    }
35
36    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
37      IDictionary dict = (IDictionary)instance;
38      IEnumerator<Tag> iter = o.GetEnumerator();
39      while (iter.MoveNext()) {
40        Tag key = iter.Current;
41        iter.MoveNext();
42        Tag value = iter.Current;
43        dict.Add(key.Value, value.Value);
44      }     
45    }
46  }
47
48}
Note: See TracBrowser for help on using the repository browser.