- Timestamp:
- 03/15/10 12:02:56 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/Configuration.cs
r3017 r3030 1 1 using System; 2 using System.Linq; 2 3 using System.Collections.Generic; 3 4 using HeuristicLab.Persistence.Interfaces; … … 11 12 /// <c>ConfigurationService</c>. 12 13 /// </summary> 13 [StorableClass] 14 [StorableClass] 14 15 public class Configuration { 15 16 … … 27 28 [Storable] 28 29 public IFormat Format { get; private set; } 29 30 private Configuration() { 30 31 [StorableConstructor] 32 private Configuration(bool isDeserializing) { 31 33 compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>(); 34 if (isDeserializing) 35 return; 36 primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>(); 37 compositeSerializers = new List<ICompositeSerializer>(); 32 38 } 33 39 … … 40 46 public Configuration(IFormat format, 41 47 IEnumerable<IPrimitiveSerializer> primitiveSerializers, 42 IEnumerable<ICompositeSerializer> compositeSerializers) { 48 IEnumerable<ICompositeSerializer> compositeSerializers) 49 : this(false) { 43 50 this.Format = format; 44 this.primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>(); 45 foreach (IPrimitiveSerializer primitiveSerializer in primitiveSerializers) { 46 if (primitiveSerializer.SerialDataType != format.SerialDataType) { 47 throw new ArgumentException("All primitive serializers must have the same IFormat."); 48 } 51 this.compositeSerializers.AddRange(compositeSerializers); 52 foreach (var primitiveSerializer in primitiveSerializers) { 53 if (primitiveSerializer.SerialDataType != format.SerialDataType) 54 throw new ArgumentException(string.Format( 55 "primitive serializer's ({0}) serialized data type ({1}) " + Environment.NewLine + 56 "is not compatible with selected format's ({2}) seriali data type ({3})", 57 primitiveSerializers.GetType().FullName, primitiveSerializer.SerialDataType.FullName, 58 format.Name, format.SerialDataType.FullName)); 49 59 this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer); 50 } 51 this.compositeSerializers = new List<ICompositeSerializer>(compositeSerializers); 52 compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>(); 60 } 53 61 } 54 62 … … 97 105 return null; 98 106 } 107 108 /// <summary> 109 /// Copies this configuration and re-instantiates all serializers. 110 /// </summary> 111 /// <returns>A new <see cref="Configuration"/></returns> 112 public Configuration Copy() { 113 var config = new Configuration(false); 114 config.Format = Format; 115 foreach (var ps in primitiveSerializers) 116 config.primitiveSerializers.Add( 117 ps.Key, 118 (IPrimitiveSerializer)Activator.CreateInstance(ps.Value.GetType())); 119 foreach (var cs in compositeSerializers) 120 config.compositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(cs.GetType())); 121 return config; 122 } 123 99 124 } 100 125 -
trunk/sources/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs
r3016 r3030 242 242 243 243 /// <summary> 244 /// Get a configuration for a certain format. This returns a custom configuration244 /// Get a configuration for a certain format. This returns a fresh copy of a custom configuration, 245 245 /// if defined, otherwise returns the default (automatically discovered) configuration. 246 246 /// </summary> … … 249 249 public Configuration GetConfiguration(IFormat format) { 250 250 if (customConfigurations.ContainsKey(format)) 251 return customConfigurations[format] ;251 return customConfigurations[format].Copy(); 252 252 return GetDefaultConfig(format); 253 253 } … … 258 258 /// <param name="configuration">The new configuration.</param> 259 259 public void DefineConfiguration(Configuration configuration) { 260 customConfigurations[configuration.Format] = configuration ;260 customConfigurations[configuration.Format] = configuration.Copy(); 261 261 SaveSettings(); 262 262 }
Note: See TracChangeset
for help on using the changeset viewer.