#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Persistence.Core;
using HeuristicLab.Persistence.Interfaces;
using System.Collections.Generic;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Persistence.Default.CompositeSerializers {
[StorableClass]
internal sealed class EnumSerializer : ICompositeSerializer {
public int Priority {
get { return 100; }
}
public bool CanSerialize(Type type) {
return type.IsEnum || type == typeof(Enum);
}
public string JustifyRejection(Type type) {
return "not an enum and not System.Enum";
}
public IEnumerable CreateMetaInfo(object obj) {
yield return new Tag(Enum.Format(obj.GetType(), obj, "G"));
}
public IEnumerable Decompose(object obj) {
return new Tag[] { };
}
public object CreateInstance(Type t, IEnumerable metaInfo) {
IEnumerator it = metaInfo.GetEnumerator();
try {
it.MoveNext();
return Enum.Parse(t, (string)it.Current.Value);
} catch (InvalidOperationException e) {
throw new PersistenceException("not enough meta information to recstruct enum", e);
} catch (InvalidCastException e) {
throw new PersistenceException("invalid meta information found while trying to reconstruct enum", e);
}
}
public void Populate(object instance, IEnumerable elements, Type t) {
// Enums are already populated during instance creation.
}
}
}