Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/TypeSerializer.cs @ 13375

Last change on this file since 13375 was 13368, checked in by ascheibe, 8 years ago

#2520 added guids to storable classes

File size: 3.1 KB
RevLine 
[13368]1#region License Information
[3742]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;
[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
[13368]31  [StorableClass("EC815939-4BC3-44D1-8464-7FCEA6D802C5")]
[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) {
[8641]43      #region Mono Compatibility
[1566]44      return type == typeof(Type) ||
[8641]45        type.VersionInvariantName() == "System.RuntimeType, mscorlib" ||
46        type.VersionInvariantName() == "System.MonoType, mscorlib";
47      #endregion
[1454]48    }
49
[2993]50    public string JustifyRejection(Type type) {
[8641]51      #region Mono Compatibility
52      return "not System.Type, System.RuntimeType, System.MonoType";
53      #endregion
[2993]54    }
55
[1553]56    public IEnumerable<Tag> CreateMetaInfo(object o) {
[1843]57      yield return new Tag("AssemblyQualifiedName", ((Type)o).AssemblyQualifiedName);
[1454]58    }
59
[1553]60    public IEnumerable<Tag> Decompose(object obj) {
61      return new Tag[] { };
[1454]62    }
63
[1553]64    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
[1625]65      IEnumerator<Tag> it = metaInfo.GetEnumerator();
66      try {
67        it.MoveNext();
[4068]68      }
69      catch (InvalidOperationException e) {
[1625]70        throw new PersistenceException("Insufficient meta information to instantiate Type object", e);
[1454]71      }
[1625]72      try {
[1843]73        return TypeLoader.Load((string)it.Current.Value);
[4068]74      }
75      catch (InvalidCastException e) {
[1625]76        throw new PersistenceException("Invalid meta information during reconstruction of Type object", e);
[4068]77      }
78      catch (TypeLoadException e) {
[1625]79        throw new PersistenceException(String.Format(
80          "Cannot load Type {0}, make sure all required assemblies are available.",
81          (string)it.Current.Value), e);
82      }
[1454]83    }
[1553]84
[1566]85    public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
[1625]86      // Type ojects are populated during instance creation.
[1553]87    }
[1454]88  }
89}
Note: See TracBrowser for help on using the repository browser.