Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3205 was 3036, checked in by epitzer, 14 years ago

make most serializers internal and complete API documentation (#548)

File size: 1.5 KB
Line 
1using System;
2using HeuristicLab.Persistence.Core;
3using HeuristicLab.Persistence.Interfaces;
4using System.Collections.Generic;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6
7namespace HeuristicLab.Persistence.Default.CompositeSerializers {
8
9  [StorableClass]
10  internal sealed class EnumSerializer : ICompositeSerializer {
11
12    public int Priority {
13      get { return 100; }
14    }
15
16    public bool CanSerialize(Type type) {
17      return type.IsEnum || type == typeof(Enum);
18    }
19
20    public string JustifyRejection(Type type) {
21      return "not an enum and not System.Enum";
22    }
23
24    public IEnumerable<Tag> CreateMetaInfo(object obj) {
25      yield return new Tag(Enum.Format(obj.GetType(), obj, "G"));
26    }
27
28    public IEnumerable<Tag> Decompose(object obj) {
29      return new Tag[] { };
30    }
31
32    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
33      IEnumerator<Tag> it = metaInfo.GetEnumerator();
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      }
42    }
43
44    public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
45      // Enums are already populated during instance creation.
46    }
47  }
48}
Note: See TracBrowser for help on using the repository browser.