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 | |
---|
1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using HeuristicLab.Persistence.Core;
|
---|
6 | using HeuristicLab.Persistence.Interfaces;
|
---|
7 |
|
---|
8 | namespace 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.