Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3742 was 3742, checked in by gkronber, 14 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

File size: 3.1 KB
Line 
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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Persistence.Interfaces;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using System.Reflection;
29using HeuristicLab.Persistence.Core;
30
31namespace HeuristicLab.Persistence.Default.CompositeSerializers {
32
33  [StorableClass]
34  internal sealed class StructSerializer : ICompositeSerializer {   
35
36    public int Priority {
37      get { return 50; }
38    }
39
40    public bool CanSerialize(Type type) {
41      return type.IsValueType && !type.IsPrimitive && !type.IsEnum && type.IsSealed;     
42    }
43
44    public string JustifyRejection(Type type) {
45      if (!type.IsValueType)
46        return "not a value type";
47      if (type.IsPrimitive)
48        return "type is primitive (int, float, ...)";
49      if (type.IsEnum)
50        return "type is enum";
51      return "type is not sealed";
52    }
53
54    public IEnumerable<Tag> CreateMetaInfo(object obj) {
55      return null;
56    }
57
58    private readonly BindingFlags AllInstanceMembers =
59      BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
60
61    public IEnumerable<Tag> Decompose(object obj) {
62      Type t = obj.GetType();
63      foreach (MemberInfo mi in t.GetMembers(AllInstanceMembers)) {
64        if (mi.MemberType == MemberTypes.Field) {
65          string name = mi.Name.Replace("<", "&lt;").Replace(">", "&gt;");         
66          yield return new Tag(name, ((FieldInfo)mi).GetValue(obj));
67        }
68      }
69    }
70
71    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
72      return Activator.CreateInstance(type, true);
73    }
74
75    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
76      foreach (Tag t in tags) {
77        string name = t.Name.Replace("&lt;", "<").Replace("&gt;", ">");
78        MemberInfo[] mis = type.GetMember(name, AllInstanceMembers);
79        if (mis.Length != 1)
80          throw new PersistenceException("ambiguous struct member name " + name);
81        MemberInfo mi = mis[0];
82        if (mi.MemberType == MemberTypes.Field)
83          ((FieldInfo)mi).SetValue(instance, t.Value);       
84        else
85          throw new PersistenceException("invalid struct member type " + mi.MemberType.ToString());
86      }
87    }
88   
89  }
90}
Note: See TracBrowser for help on using the repository browser.