Last change
on this file since 3199 was
3036,
checked in by epitzer, 15 years ago
|
make most serializers internal and complete API documentation (#548)
|
File size:
1.5 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 | [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.