#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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 System.Reflection;
using HeuristicLab.Persistence.Data;
namespace HeuristicLab.Persistence {
[Transformer("A2A2F675-012B-4D50-93A5-B8E728D9809C", 999)]
internal sealed class EmptyTypeTransformer : TransformerBase {
public override bool CanTransformType(Type type) {
while (type != null) {
if (type.IsArray) return false; // arrays are no empty types but contain no fields
var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
if (fields.Length != 0) return false;
type = type.BaseType;
}
return true;
}
public override PersistenceData ToData(object o, PersistenceMapper mapper) {
var type = o.GetType();
var data = new ValueData(Id);
mapper.Cache(o, data);
data.Value = mapper.GetTypeId(type);
return data;
}
public override object ToObject(PersistenceData data, PersistenceMapper mapper) {
var primitive = (ValueData)data;
var o = Activator.CreateInstance(mapper.GetType(primitive.Value));
mapper.Cache(data, o);
return o;
}
}
}