Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs @ 1553

Last change on this file since 1553 was 1553, checked in by epitzer, 15 years ago

Replace final fixes for broken parent references with separation of instance creation with meta information. (#548)

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