Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

File size: 1.5 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
[3017]9  [StorableClass]
[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
[2993]20    public string JustifyRejection(Type type) {
21      return "not an enum and not System.Enum";
22    }
23
[1553]24    public IEnumerable<Tag> CreateMetaInfo(object obj) {
[1684]25      yield return new Tag(Enum.Format(obj.GetType(), obj, "G"));
[1456]26    }
27
[1553]28    public IEnumerable<Tag> Decompose(object obj) {
29      return new Tag[] { };
[1456]30    }
[1553]31
32    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
33      IEnumerator<Tag> it = metaInfo.GetEnumerator();
[1625]34      try {
35        it.MoveNext();
36        return Enum.Parse(t, (string)it.Current.Value);
37      } catch (InvalidOperationException e) {
38        throw new PersistenceException("not enough meta information to recstruct enum", e);
39      } catch (InvalidCastException e) {
40        throw new PersistenceException("invalid meta information found while trying to reconstruct enum", e);
41      }
[1456]42    }
[1566]43
44    public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
[1625]45      // Enums are already populated during instance creation.
[1553]46    }
[1456]47  }
[1684]48}
Note: See TracBrowser for help on using the repository browser.