Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumSerializer.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.3 KB
Line 
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;
23using HeuristicLab.Persistence.Core;
24using HeuristicLab.Persistence.Interfaces;
25using System.Collections.Generic;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Persistence.Default.CompositeSerializers {
29
30  [StorableClass]
31  internal sealed class EnumSerializer : ICompositeSerializer {
32
33    public int Priority {
34      get { return 100; }
35    }
36
37    public bool CanSerialize(Type type) {
38      return type.IsEnum || type == typeof(Enum);
39    }
40
41    public string JustifyRejection(Type type) {
42      return "not an enum and not System.Enum";
43    }
44
45    public IEnumerable<Tag> CreateMetaInfo(object obj) {
46      yield return new Tag(Enum.Format(obj.GetType(), obj, "G"));
47    }
48
49    public IEnumerable<Tag> Decompose(object obj) {
50      return new Tag[] { };
51    }
52
53    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
54      IEnumerator<Tag> it = metaInfo.GetEnumerator();
55      try {
56        it.MoveNext();
57        return Enum.Parse(t, (string)it.Current.Value);
58      } catch (InvalidOperationException e) {
59        throw new PersistenceException("not enough meta information to recstruct enum", e);
60      } catch (InvalidCastException e) {
61        throw new PersistenceException("invalid meta information found while trying to reconstruct enum", e);
62      }
63    }
64
65    public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
66      // Enums are already populated during instance creation.
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.