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.3 KB
|
Line | |
---|
1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Reflection;
|
---|
4 | using HeuristicLab.Persistence.Core;
|
---|
5 | using HeuristicLab.Persistence.Interfaces;
|
---|
6 | using System.Collections.Generic;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Persistence.Default.Decomposers {
|
---|
9 |
|
---|
10 | [EmptyStorableClass]
|
---|
11 | public class EnumerableDecomposer : IDecomposer {
|
---|
12 |
|
---|
13 | public bool CanDecompose(Type type) {
|
---|
14 | return
|
---|
15 | type.GetInterface(typeof(IEnumerable).FullName) != null &&
|
---|
16 | type.GetMethod("Add") != null &&
|
---|
17 | type.GetMethod("Add").GetParameters().Length == 1 &&
|
---|
18 | type.GetConstructor(
|
---|
19 | BindingFlags.Public |
|
---|
20 | BindingFlags.NonPublic |
|
---|
21 | BindingFlags.Instance,
|
---|
22 | null, Type.EmptyTypes, null) != null;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public IEnumerable<Tag> DeCompose(object obj) {
|
---|
26 | foreach (object o in (IEnumerable)obj) {
|
---|
27 | yield return new Tag(null, o);
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | public object CreateInstance(Type type) {
|
---|
32 | return Activator.CreateInstance(type, true);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public object Populate(object instance, IEnumerable<Tag> objects, Type type) {
|
---|
36 | MethodInfo addMethod = type.GetMethod("Add");
|
---|
37 | foreach (var pair in objects) {
|
---|
38 | addMethod.Invoke(instance, new[] {pair.Value});
|
---|
39 | }
|
---|
40 | return instance;
|
---|
41 | }
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|
45 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.