Exemplo n.º 1
0
 /// <summary>
 /// Copies this configuration and re-instantiates all serializers.
 /// </summary>
 /// <returns>A new <see cref="Configuration"/></returns>
 public Configuration Copy() {
   var config = new Configuration(false);
   config.Format = Format;
   foreach (var ps in primitiveSerializers)
     config.primitiveSerializers.Add(
       ps.Key,
       (IPrimitiveSerializer)Activator.CreateInstance(ps.Value.GetType()));
   foreach (var cs in compositeSerializers)
     config.compositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(cs.GetType()));
   return config;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Serializes the specified object into a file.
 /// Needed assemblies are not included, ZIP compression level is set to 5.
 /// </summary>
 /// <param name="obj">The object.</param>
 /// <param name="filename">The filename.</param>
 /// <param name="config">The configuration.</param>
 public new static void Serialize(object obj, string filename, Configuration config) {
   try {
     string tempfile = Path.GetTempFileName();
     DateTime start = DateTime.Now;
     using (FileStream stream = File.Create(tempfile)) {
       Serialize(obj, stream, config);
     }
     Logger.Info(String.Format("easy serialization took {0} seconds",
       (DateTime.Now - start).TotalSeconds));
     File.Copy(tempfile, filename, true);
     File.Delete(tempfile);
   }
   catch (Exception) {
     Logger.Warn("Exception caught, no data has been written.");
     throw;
   }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Serializes the specified object into a stream.
 /// </summary>
 /// <param name="obj">The object.</param>
 /// <param name="stream">The stream.</param>
 /// <param name="config">The configuration.</param>
 public static void Serialize(object obj, Stream stream, Configuration config) {
   try {
     using (StreamWriter writer = new StreamWriter(stream)) {
       Serializer serializer = new Serializer(obj, config);
       serializer.InterleaveTypeInformation = true;
       ReadableXmlGenerator generator = new ReadableXmlGenerator();
       foreach (ISerializationToken token in serializer) {
         string line = generator.Format(token);
         writer.Write(line);
       }
       writer.Flush();
     }
   }
   catch (PersistenceException) {
     throw;
   }
   catch (Exception e) {
     throw new PersistenceException("Unexpected exception during Serialization.", e);
   }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Define a new custom configuration for a ceratin format.
 /// </summary>
 /// <param name="configuration">The new configuration.</param>
 public void DefineConfiguration(Configuration configuration)
 {
     customConfigurations[configuration.Format] = configuration.Copy();
     SaveSettings();
 }