Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumSerializer.cs @ 1960

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

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 1.4 KB
RevLine 
[1456]1using System;
2using HeuristicLab.Persistence.Core;
3using HeuristicLab.Persistence.Interfaces;
4using System.Collections.Generic;
[1823]5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1456]6
[1823]7namespace HeuristicLab.Persistence.Default.CompositeSerializers {
[1566]8
[1563]9  [EmptyStorableClass]
[1823]10  public class EnumSerializer : ICompositeSerializer {
[1456]11
[1539]12    public int Priority {
13      get { return 100; }
14    }
15
[1823]16    public bool CanSerialize(Type type) {
[1542]17      return type.IsEnum || type == typeof(Enum);
[1456]18    }
19
[1553]20    public IEnumerable<Tag> CreateMetaInfo(object obj) {
[1684]21      yield return new Tag(Enum.Format(obj.GetType(), obj, "G"));
[1456]22    }
23
[1553]24    public IEnumerable<Tag> Decompose(object obj) {
25      return new Tag[] { };
[1456]26    }
[1553]27
28    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
29      IEnumerator<Tag> it = metaInfo.GetEnumerator();
[1625]30      try {
31        it.MoveNext();
32        return Enum.Parse(t, (string)it.Current.Value);
33      } catch (InvalidOperationException e) {
34        throw new PersistenceException("not enough meta information to recstruct enum", e);
35      } catch (InvalidCastException e) {
36        throw new PersistenceException("invalid meta information found while trying to reconstruct enum", e);
37      }
[1456]38    }
[1566]39
40    public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
[1625]41      // Enums are already populated during instance creation.
[1553]42    }
[1456]43  }
[1684]44}
Note: See TracBrowser for help on using the repository browser.