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 int Priority {
|
---|
14 | get { return 100; }
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 | public bool CanDecompose(Type type) {
|
---|
19 | return
|
---|
20 | type.GetInterface(typeof(IEnumerable).FullName) != null &&
|
---|
21 | type.GetMethod("Add") != null &&
|
---|
22 | type.GetMethod("Add").GetParameters().Length == 1 &&
|
---|
23 | type.GetConstructor(
|
---|
24 | BindingFlags.Public |
|
---|
25 | BindingFlags.NonPublic |
|
---|
26 | BindingFlags.Instance,
|
---|
27 | null, Type.EmptyTypes, null) != null;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
31 | return new Tag[] { };
|
---|
32 | }
|
---|
33 |
|
---|
34 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
35 | foreach (object o in (IEnumerable)obj) {
|
---|
36 | yield return new Tag(null, o);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
41 | return Activator.CreateInstance(type, true);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
45 | MethodInfo addMethod = type.GetMethod("Add");
|
---|
46 | foreach (var tag in tags)
|
---|
47 | addMethod.Invoke(instance, new[] { tag.Value });
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.