Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1563 was 1563, checked in by epitzer, 16 years ago

Mark decomposers as empty storable class. (#548)

File size: 1.4 KB
RevLine 
[1454]1using System;
2using System.Collections;
3using System.Reflection;
4using HeuristicLab.Persistence.Core;
5using HeuristicLab.Persistence.Interfaces;
6using System.Collections.Generic;
7
[1553]8namespace 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.