Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/13/11 13:01:28 (14 years ago)
Author:
epitzer
Message:

Added custom serializers for generic HashSets and Dictionaries that include the Comparer property (#1375)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers
Files:
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/ConcreteDictionarySerializer.cs

    r5289 r5290  
    2323using System.Collections;
    2424using System.Collections.Generic;
    25 using HeuristicLab.Persistence.Auxiliary;
     25using System.Linq;
    2626using HeuristicLab.Persistence.Core;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3131
    3232  [StorableClass]
    33   internal sealed class DictionarySerializer : ICompositeSerializer {
     33  internal sealed class ConcreteDictionarySerializer : ICompositeSerializer {
    3434
    3535    [StorableConstructor]
    36     private DictionarySerializer(bool deserializing) { }
    37     public DictionarySerializer() { }
     36    private ConcreteDictionarySerializer(bool deserializing) { }
     37    public ConcreteDictionarySerializer() { }
    3838
    3939    public int Priority {
     
    4343
    4444    public bool CanSerialize(Type type) {
    45       return ReflectionTools.HasDefaultConstructor(type) &&
    46         type.GetInterface(typeof(IDictionary).FullName) != null;
     45      return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>);
    4746    }
    4847
    4948    public string JustifyRejection(Type type) {
    50       if (!ReflectionTools.HasDefaultConstructor(type))
    51         return "no default constructor";
    52       return "interface IDictionary not implemented";
     49      return "Type is not a generic Dictionary<>";
    5350    }
    5451
    5552    public IEnumerable<Tag> CreateMetaInfo(object o) {
    56       return new Tag[] { };
     53      yield return new Tag("Comparer", o.GetType().GetProperty("Comparer").GetValue(o, null));
    5754    }
    5855
     
    6663
    6764    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
    68       return Activator.CreateInstance(t, true);
     65      return Activator.CreateInstance(t, metaInfo.First().Value);
    6966    }
    7067
     
    7976          dict.Add(key.Value, value.Value);
    8077        }
    81       }
    82       catch (InvalidOperationException e) {
     78      } catch (InvalidOperationException e) {
    8379        throw new PersistenceException("Dictionaries must contain an even number of elements (key+value).", e);
    84       }
    85       catch (NotSupportedException e) {
     80      } catch (NotSupportedException e) {
    8681        throw new PersistenceException("The serialized dictionary type was read-only or had a fixed size and cannot be deserialized.", e);
    87       }
    88       catch (ArgumentNullException e) {
     82      } catch (ArgumentNullException e) {
    8983        throw new PersistenceException("Dictionary key was null.", e);
    90       }
    91       catch (ArgumentException e) {
     84      } catch (ArgumentException e) {
    9285        throw new PersistenceException("Duplicate dictionary key.", e);
    9386      }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/HashSetSerializer.cs

    r5289 r5290  
    2323using System.Collections;
    2424using System.Collections.Generic;
     25using System.Linq;
    2526using System.Reflection;
    26 using HeuristicLab.Persistence.Auxiliary;
    2727using HeuristicLab.Persistence.Core;
    2828using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3232
    3333  [StorableClass]
    34   internal sealed class EnumerableSerializer : ICompositeSerializer {
     34  internal sealed class HashSetSerializer : ICompositeSerializer {
    3535
    3636    [StorableConstructor]
    37     private EnumerableSerializer(bool deserializing) { }
    38     public EnumerableSerializer() { }
     37    private HashSetSerializer(bool deserializing) { }
     38    public HashSetSerializer() { }
    3939
    4040    public int Priority {
    41       get { return 100; }
     41      get { return 250; }
    4242    }
    4343
    44 
    4544    public bool CanSerialize(Type type) {
    46       return
    47         ReflectionTools.HasDefaultConstructor(type) &&
    48         type.GetInterface(typeof(IEnumerable).FullName) != null &&
    49         type.GetMethod("Add") != null &&
    50         type.GetMethod("Add").GetParameters().Length == 1;
     45      return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(HashSet<>);
    5146    }
    5247
    5348    public string JustifyRejection(Type type) {
    54       if (!ReflectionTools.HasDefaultConstructor(type))
    55         return "no default constructor";
    56       if (type.GetInterface(typeof(IEnumerable).FullName) == null)
    57         return "interface IEnumerable not implemented";
    58       if (type.GetMethod("Add") == null)
    59         return "no 'Add()' method";
    60       return "no 'Add()' method with one argument";
     49      return "Type is not a generic HashSet<>";
    6150    }
    6251
    6352    public IEnumerable<Tag> CreateMetaInfo(object o) {
    64       return new Tag[] { };
     53      yield return new Tag("Comparer", o.GetType().GetProperty("Comparer").GetValue(o, null));
    6554    }
    6655
     
    7261
    7362    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    74       return Activator.CreateInstance(type, true);
     63      return Activator.CreateInstance(type, metaInfo.First().Value);
    7564    }
    7665
     
    8069        foreach (var tag in tags)
    8170          addMethod.Invoke(instance, new[] { tag.Value });
    82       }
    83       catch (Exception e) {
     71      } catch (Exception e) {
    8472        throw new PersistenceException("Exception caught while trying to populate enumerable.", e);
    8573      }
    8674    }
     75
     76    public object HashSet { get; set; }
    8777  }
    8878}
Note: See TracChangeset for help on using the changeset viewer.