Free cookie consent management tool by TermsFeed Policy Generator

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

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

Split all classes into their own file (#506)

File size: 1.1 KB
Line 
1using System;
2using System.Collections;
3using System.Reflection;
4using HeuristicLab.Persistence.Interfaces;
5
6namespace 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.