Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/DictionaryDecomposer.cs @ 1419

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

Implement persistence of storables as decomposer. (#506)

File size: 1.2 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 bool CanDecompose(Type type) {
13      return type.GetInterface(typeof(IDictionary).FullName) != null;       
14    }
15
16    public IEnumerable<Tag> DeCompose(object o) {
17      IDictionary dict = (IDictionary)o;     
18      foreach ( DictionaryEntry entry in dict) {
19        yield return new Tag("key", entry.Key);
20        yield return new Tag("value", entry.Value);
21      }
22    }
23
24    public object CreateInstance(Type t) {
25      return Activator.CreateInstance(t, true);
26    }
27
28    public object Populate(object instance, IEnumerable<Tag> o, Type t) {
29      IDictionary dict = (IDictionary)instance;
30      IEnumerator<Tag> iter = o.GetEnumerator();
31      while (iter.MoveNext()) {
32        object key = iter.Current.Value;
33        iter.MoveNext();
34        object value = iter.Current.Value;
35        dict.Add(key, value);
36      }
37      return dict;
38    }
39  }
40
41}
Note: See TracBrowser for help on using the repository browser.