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