Last change
on this file since 2227 was
1823,
checked in by epitzer, 16 years ago
|
Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)
|
File size:
1.4 KB
|
Line | |
---|
1 | using System;
|
---|
2 | using HeuristicLab.Persistence.Core;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
8 |
|
---|
9 | [EmptyStorableClass]
|
---|
10 | public 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 IEnumerable<Tag> CreateMetaInfo(object obj) {
|
---|
21 | yield return new Tag(Enum.Format(obj.GetType(), obj, "G"));
|
---|
22 | }
|
---|
23 |
|
---|
24 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
25 | return new Tag[] { };
|
---|
26 | }
|
---|
27 |
|
---|
28 | public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
|
---|
29 | IEnumerator<Tag> it = metaInfo.GetEnumerator();
|
---|
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 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void Populate(object instance, IEnumerable<Tag> elements, Type t) {
|
---|
41 | // Enums are already populated during instance creation.
|
---|
42 | }
|
---|
43 | }
|
---|
44 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.