Last change
on this file since 1405 was
1405,
checked in by epitzer, 15 years ago
|
Add empty storable class attributes to classes (#506)
|
File size:
1010 bytes
|
Line | |
---|
1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using HeuristicLab.Persistence.Core;
|
---|
4 | using HeuristicLab.Persistence.Interfaces;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Persistence.Default.Decomposers {
|
---|
7 |
|
---|
8 | [EmptyStorableClass]
|
---|
9 | public class DictionaryDecomposer : IDecomposer {
|
---|
10 |
|
---|
11 | public bool CanDecompose(Type type) {
|
---|
12 | return type.GetInterface(typeof(IDictionary).FullName) != null;
|
---|
13 | }
|
---|
14 |
|
---|
15 | public IEnumerable DeCompose(object o) {
|
---|
16 | IDictionary dict = (IDictionary)o;
|
---|
17 | foreach ( DictionaryEntry entry in dict) {
|
---|
18 | yield return entry.Key;
|
---|
19 | yield return entry.Value;
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public object Compose(IEnumerable o, Type t) {
|
---|
24 | IDictionary dict = (IDictionary)Activator.CreateInstance(t, true);
|
---|
25 | IEnumerator iter = o.GetEnumerator();
|
---|
26 | while (iter.MoveNext()) {
|
---|
27 | object key = iter.Current;
|
---|
28 | iter.MoveNext();
|
---|
29 | object value = iter.Current;
|
---|
30 | dict.Add(key, value);
|
---|
31 | }
|
---|
32 | return dict;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.