Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13401 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.2 KB
RevLine 
[3742]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3742]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;
[2939]23using System.Collections.Generic;
24using System.Reflection;
25using HeuristicLab.Persistence.Core;
[4068]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Persistence.Interfaces;
[2939]28
29namespace HeuristicLab.Persistence.Default.CompositeSerializers {
30
[3017]31  [StorableClass]
[4068]32  internal sealed class StructSerializer : ICompositeSerializer {
[2939]33
[4806]34    [StorableConstructor]
35    private StructSerializer(bool deserializing) { }
36    public StructSerializer() { }
37
[2939]38    public int Priority {
39      get { return 50; }
40    }
41
42    public bool CanSerialize(Type type) {
[4068]43      return type.IsValueType && !type.IsPrimitive && !type.IsEnum && type.IsSealed;
[2939]44    }
45
[2993]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
[2939]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) {
[4068]67          string name = mi.Name.Replace("<", "&lt;").Replace(">", "&gt;");
[2939]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)
[4068]85          ((FieldInfo)mi).SetValue(instance, t.Value);
[2939]86        else
87          throw new PersistenceException("invalid struct member type " + mi.MemberType.ToString());
88      }
89    }
[4068]90
[2939]91  }
92}
Note: See TracBrowser for help on using the repository browser.