Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/StructSerializer.cs @ 11110

Last change on this file since 11110 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Reflection;
25using HeuristicLab.Persistence.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Persistence.Interfaces;
28
29namespace HeuristicLab.Persistence.Default.CompositeSerializers {
30
31  [StorableClass]
32  internal sealed class StructSerializer : ICompositeSerializer {
33
34    [StorableConstructor]
35    private StructSerializer(bool deserializing) { }
36    public StructSerializer() { }
37
38    public int Priority {
39      get { return 50; }
40    }
41
42    public bool CanSerialize(Type type) {
43      return type.IsValueType && !type.IsPrimitive && !type.IsEnum && type.IsSealed;
44    }
45
46    public string JustifyRejection(Type type) {
47      if (!type.IsValueType)
48        return "not a value type";
49      if (type.IsPrimitive)
50        return "type is primitive (int, float, ...)";
51      if (type.IsEnum)
52        return "type is enum";
53      return "type is not sealed";
54    }
55
56    public IEnumerable<Tag> CreateMetaInfo(object obj) {
57      return null;
58    }
59
60    private readonly BindingFlags AllInstanceMembers =
61      BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
62
63    public IEnumerable<Tag> Decompose(object obj) {
64      Type t = obj.GetType();
65      foreach (MemberInfo mi in t.GetMembers(AllInstanceMembers)) {
66        if (mi.MemberType == MemberTypes.Field) {
67          string name = mi.Name.Replace("<", "&lt;").Replace(">", "&gt;");
68          yield return new Tag(name, ((FieldInfo)mi).GetValue(obj));
69        }
70      }
71    }
72
73    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
74      return Activator.CreateInstance(type, true);
75    }
76
77    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
78      foreach (Tag t in tags) {
79        string name = t.Name.Replace("&lt;", "<").Replace("&gt;", ">");
80        MemberInfo[] mis = type.GetMember(name, AllInstanceMembers);
81        if (mis.Length != 1)
82          throw new PersistenceException("ambiguous struct member name " + name);
83        MemberInfo mi = mis[0];
84        if (mi.MemberType == MemberTypes.Field)
85          ((FieldInfo)mi).SetValue(instance, t.Value);
86        else
87          throw new PersistenceException("invalid struct member type " + mi.MemberType.ToString());
88      }
89    }
90
91  }
92}
Note: See TracBrowser for help on using the repository browser.