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