Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4695 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.1 KB
RevLine 
[3742]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
[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
34    public int Priority {
35      get { return 50; }
36    }
37
38    public bool CanSerialize(Type type) {
[4068]39      return type.IsValueType && !type.IsPrimitive && !type.IsEnum && type.IsSealed;
[2939]40    }
41
[2993]42    public string JustifyRejection(Type type) {
43      if (!type.IsValueType)
44        return "not a value type";
45      if (type.IsPrimitive)
46        return "type is primitive (int, float, ...)";
47      if (type.IsEnum)
48        return "type is enum";
49      return "type is not sealed";
50    }
51
[2939]52    public IEnumerable<Tag> CreateMetaInfo(object obj) {
53      return null;
54    }
55
56    private readonly BindingFlags AllInstanceMembers =
57      BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
58
59    public IEnumerable<Tag> Decompose(object obj) {
60      Type t = obj.GetType();
61      foreach (MemberInfo mi in t.GetMembers(AllInstanceMembers)) {
62        if (mi.MemberType == MemberTypes.Field) {
[4068]63          string name = mi.Name.Replace("<", "&lt;").Replace(">", "&gt;");
[2939]64          yield return new Tag(name, ((FieldInfo)mi).GetValue(obj));
65        }
66      }
67    }
68
69    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
70      return Activator.CreateInstance(type, true);
71    }
72
73    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
74      foreach (Tag t in tags) {
75        string name = t.Name.Replace("&lt;", "<").Replace("&gt;", ">");
76        MemberInfo[] mis = type.GetMember(name, AllInstanceMembers);
77        if (mis.Length != 1)
78          throw new PersistenceException("ambiguous struct member name " + name);
79        MemberInfo mi = mis[0];
80        if (mi.MemberType == MemberTypes.Field)
[4068]81          ((FieldInfo)mi).SetValue(instance, t.Value);
[2939]82        else
83          throw new PersistenceException("invalid struct member type " + mi.MemberType.ToString());
84      }
85    }
[4068]86
[2939]87  }
88}
Note: See TracBrowser for help on using the repository browser.