Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Core/3.3/PersistenceManager.cs @ 4501

Last change on this file since 4501 was 1734, checked in by epitzer, 16 years ago

Serialization with streams. (#603)

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.Xml;
26using System.IO;
27using System.IO.Compression;
28using HeuristicLab.PluginInfrastructure;
29using HeuristicLab.Persistence.Default.Xml;
30using HeuristicLab.Persistence.Core;
31
32namespace HeuristicLab.Core {
33  /// <summary>
34  /// Static class for serializing and deserializing objects.
35  /// </summary>
36  public static class PersistenceManager {
37    /// <summary>
38    /// Saves the specified <paramref name="instance"/> in the specified file through creating an
39    /// <see cref="XmlDocument"/>.
40    /// </summary>
41    /// <param name="instance">The object that should be saved.</param>
42    /// <param name="filename">The name of the file where the <paramref name="object"/> should be saved.</param>
43    public static void Save(IStorable instance, string filename) {
44      XmlGenerator.Serialize(instance, filename);
45    }
46
47    /// <summary>
48    /// Saves the specified <paramref name="instance"/> in the specified <paramref name="stream"/>
49    /// through creating an <see cref="XmlDocument"/>.
50    /// </summary>
51    /// <param name="instance">The object that should be saved.</param>
52    /// <param name="stream">The (file) stream where the object should be saved.</param>
53    public static void Save(IStorable instance, Stream stream) {
54      XmlGenerator.Serialize(instance, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));     
55    }
56    /// <summary>
57    /// Loads an object from a file with the specified <paramref name="filename"/>.
58    /// </summary>
59    /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
60    /// Calls <see cref="Restore"/>.</remarks>
61    /// <param name="filename">The filename of the file where the data is saved.</param>
62    /// <returns>The loaded object.</returns>
63    public static IStorable Load(string filename) {
64      return (IStorable)XmlParser.Deserialize(filename);
65    }
66    /// <summary>
67    /// Loads an object from the specified <paramref name="stream"/>.
68    /// </summary>
69    /// <remarks>The object must be saved as an <see cref="XmlDocument"/>. <br/>
70    /// Calls <see cref="Restore"/>.</remarks>
71    /// <param name="stream">The stream from where to load the data.</param>
72    /// <returns>The loaded object.</returns>
73    public static IStorable Load(Stream stream) {
74      return (IStorable)XmlParser.Deserialize(stream);
75    }
76
77    /// <summary>
78    /// Builds a meaningful string for the given <paramref name="type"/> with the namespace information,
79    /// all its arguments, the assembly name...
80    /// </summary>
81    /// <param name="type">The type for which a string should be created.</param>
82    /// <returns>A string value of this type containing different additional information.</returns>
83    public static string BuildTypeString(Type type) {
84      string assembly = type.Assembly.FullName;
85      assembly = assembly.Substring(0, assembly.IndexOf(", "));
86
87      StringBuilder builder = new StringBuilder();
88      builder.Append(type.Namespace);
89      builder.Append(".");
90      builder.Append(type.Name);
91      Type[] args = type.GetGenericArguments();
92      if (args.Length > 0) {
93        builder.Append("[[");
94        builder.Append(BuildTypeString(args[0]));
95        builder.Append("]");
96        for (int i = 1; i < args.Length; i++) {
97          builder.Append(",[");
98          builder.Append(BuildTypeString(args[i]));
99          builder.Append("]");
100        }
101        builder.Append("]");
102      }
103      builder.Append(", ");
104      builder.Append(assembly);
105      return builder.ToString();
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.