Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumSerializer.cs @ 6409

Last change on this file since 6409 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 2.4 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 System.Collections.Generic;
24using HeuristicLab.Persistence.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Persistence.Interfaces;
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      }
59      catch (InvalidOperationException e) {
60        throw new PersistenceException("not enough meta information to recstruct enum", e);
61      }
62      catch (InvalidCastException e) {
63        throw new PersistenceException("invalid meta information found while trying to reconstruct enum", e);
64      }
65    }
66
67    public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
68      // Enums are already populated during instance creation.
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.