Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/EnumerableDecomposer.cs @ 1421

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

Remove unnecessary class attributes. (#506)

File size: 1.3 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 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.