Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/TypeSerializer.cs @ 4806

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

Added storable constructors in HeuristicLab.Persistence plugin (#922)

File size: 2.9 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;
[4068]23using System.Collections.Generic;
24using HeuristicLab.Persistence.Auxiliary;
[1454]25using HeuristicLab.Persistence.Core;
[4068]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1454]27using HeuristicLab.Persistence.Interfaces;
28
[1823]29namespace HeuristicLab.Persistence.Default.CompositeSerializers {
[1566]30
[3017]31  [StorableClass]
[3036]32  internal sealed class TypeSerializer : ICompositeSerializer {
[1539]33
[4806]34    [StorableConstructor]
35    private TypeSerializer(bool deserializing) { }
36    public TypeSerializer() { }
37
[1539]38    public int Priority {
39      get { return 100; }
40    }
41
[1823]42    public bool CanSerialize(Type type) {
[1566]43      return type == typeof(Type) ||
[1454]44             type.VersionInvariantName() == "System.RuntimeType, mscorlib";
45    }
46
[2993]47    public string JustifyRejection(Type type) {
48      return "not System.Type nor System.RuntimeType";
49    }
50
[1553]51    public IEnumerable<Tag> CreateMetaInfo(object o) {
[1843]52      yield return new Tag("AssemblyQualifiedName", ((Type)o).AssemblyQualifiedName);
[1454]53    }
54
[1553]55    public IEnumerable<Tag> Decompose(object obj) {
56      return new Tag[] { };
[1454]57    }
58
[1553]59    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
[1625]60      IEnumerator<Tag> it = metaInfo.GetEnumerator();
61      try {
62        it.MoveNext();
[4068]63      }
64      catch (InvalidOperationException e) {
[1625]65        throw new PersistenceException("Insufficient meta information to instantiate Type object", e);
[1454]66      }
[1625]67      try {
[1843]68        return TypeLoader.Load((string)it.Current.Value);
[4068]69      }
70      catch (InvalidCastException e) {
[1625]71        throw new PersistenceException("Invalid meta information during reconstruction of Type object", e);
[4068]72      }
73      catch (TypeLoadException e) {
[1625]74        throw new PersistenceException(String.Format(
75          "Cannot load Type {0}, make sure all required assemblies are available.",
76          (string)it.Current.Value), e);
77      }
[1454]78    }
[1553]79
[1566]80    public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
[1625]81      // Type ojects are populated during instance creation.
[1553]82    }
[1454]83  }
84}
Note: See TracBrowser for help on using the repository browser.