Rev | Line | |
---|
[1454] | 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 |
|
---|
[1553] | 8 | namespace HeuristicLab.Persistence.Default.Decomposers {
|
---|
[1454] | 9 |
|
---|
[1563] | 10 | [EmptyStorableClass]
|
---|
[1539] | 11 | public class EnumerableDecomposer : IDecomposer {
|
---|
[1454] | 12 |
|
---|
[1539] | 13 | public int Priority {
|
---|
| 14 | get { return 100; }
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 |
|
---|
[1454] | 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 |
|
---|
[1553] | 30 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
| 31 | return new Tag[] { };
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[1542] | 34 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
[1454] | 35 | foreach (object o in (IEnumerable)obj) {
|
---|
| 36 | yield return new Tag(null, o);
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[1553] | 40 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
[1454] | 41 | return Activator.CreateInstance(type, true);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[1553] | 44 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
[1454] | 45 | MethodInfo addMethod = type.GetMethod("Add");
|
---|
[1553] | 46 | foreach (var tag in tags)
|
---|
| 47 | addMethod.Invoke(instance, new[] { tag.Value });
|
---|
[1454] | 48 | }
|
---|
[1553] | 49 | }
|
---|
[1454] | 50 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.