Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/KeyValuePairDecomposer.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.Linq;
3using System.Collections;
4using System.Collections.Generic;
5using HeuristicLab.Persistence.Core;
6using HeuristicLab.Persistence.Interfaces;
7
8namespace HeuristicLab.Persistence.Default.Decomposers {
9
10  [EmptyStorableClass]
11  public class KeyValuePairDecomposer : IDecomposer {   
12
13    public bool CanDecompose(Type type) {
14      return type.IsGenericType &&
15             type.GetGenericTypeDefinition() ==
16             typeof (KeyValuePair<int, int>).GetGenericTypeDefinition();
17    }
18
19    public IEnumerable<Tag> DeCompose(object o) {     
20      Type t = o.GetType();
21      yield return new Tag("key", t.GetProperty("Key").GetValue(o, null));
22      yield return new Tag("value", t.GetProperty("Value").GetValue(o, null));
23    }
24
25    public object CreateInstance(Type type) {
26      return null;
27    }
28
29    public object Populate(object instance, IEnumerable<Tag> o, Type t) {
30      IEnumerator<Tag> iter = o.GetEnumerator();
31      iter.MoveNext();
32      object key = iter.Current.Value;
33      iter.MoveNext();
34      object value = iter.Current.Value;
35      return Activator.CreateInstance(t, new object[] { key, value });     
36    }   
37  }
38
39}
Note: See TracBrowser for help on using the repository browser.