Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumerableSerializer.cs @ 4047

Last change on this file since 4047 was 3742, checked in by gkronber, 15 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

File size: 2.8 KB
RevLine 
[3742]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[1454]23using System.Collections;
24using System.Reflection;
25using HeuristicLab.Persistence.Core;
26using HeuristicLab.Persistence.Interfaces;
27using System.Collections.Generic;
[1823]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1705]29using HeuristicLab.Persistence.Auxiliary;
[1454]30
[1823]31namespace HeuristicLab.Persistence.Default.CompositeSerializers {
[1454]32
[3017]33  [StorableClass]
[3036]34  internal sealed class EnumerableSerializer : ICompositeSerializer {
[1454]35
[1539]36    public int Priority {
37      get { return 100; }
38    }
39
40
[1823]41    public bool CanSerialize(Type type) {
[1454]42      return
[1705]43        ReflectionTools.HasDefaultConstructor(type) &&
[1454]44        type.GetInterface(typeof(IEnumerable).FullName) != null &&
45        type.GetMethod("Add") != null &&
[1790]46        type.GetMethod("Add").GetParameters().Length == 1;
[1454]47    }
48
[2993]49    public string JustifyRejection(Type type) {
50      if (!ReflectionTools.HasDefaultConstructor(type))
51        return "no default constructor";
52      if (type.GetInterface(typeof(IEnumerable).FullName) == null)
53        return "interface IEnumerable not implemented";
54      if (type.GetMethod("Add") == null)
55        return "no 'Add()' method";     
56      return "no 'Add()' method with one argument";
57    }
58
[1553]59    public IEnumerable<Tag> CreateMetaInfo(object o) {
60      return new Tag[] { };
61    }
62
[1542]63    public IEnumerable<Tag> Decompose(object obj) {
[1454]64      foreach (object o in (IEnumerable)obj) {
[1790]65        yield return new Tag(o);
[1454]66      }
67    }
68
[1553]69    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
[1454]70      return Activator.CreateInstance(type, true);
71    }
72
[1553]73    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
[1454]74      MethodInfo addMethod = type.GetMethod("Add");
[1625]75      try {
76        foreach (var tag in tags)
77          addMethod.Invoke(instance, new[] { tag.Value });
78      } catch (Exception e) {
79        throw new PersistenceException("Exception caught while trying to populate enumerable.", e);
80      }
[1454]81    }
[1553]82  }
[1790]83}
Note: See TracBrowser for help on using the repository browser.