Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/29/09 13:52:20 (16 years ago)
Author:
epitzer
Message:

Replace value comparison with references comparison in serializer. (#605)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r1679 r1701  
    77using HeuristicLab.Persistence.Default.Decomposers.Storable;
    88using System.Text;
     9using System.Runtime.InteropServices;
    910
    1011namespace HeuristicLab.Persistence.Core {
     12
     13  public class ReferenceEqualityComparer : IEqualityComparer<object> {
     14
     15    public bool Equals(object a, object b) {
     16      return Object.ReferenceEquals(a, b);
     17    }
     18
     19    public int GetHashCode(object obj) {
     20      GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Weak);
     21      int address = GCHandle.ToIntPtr(handle).ToInt32();
     22      handle.Free();
     23      return address;
     24    }   
     25
     26  }
    1127
    1228  public class Serializer : IEnumerable<ISerializationToken> {
     
    4359      this.rootName = rootName;
    4460      this.configuration = configuration;
    45       obj2id = new Dictionary<object, int> { { new object(), 0 } };
     61      obj2id = new Dictionary<object,int>(new ReferenceEqualityComparer()) { { new object(), 0 } };
    4662      typeCache = new Dictionary<Type, int>();
    4763    }
     
    5975      if (value == null)
    6076        return NullReferenceEnumerator(accessor.Name);
     77      Type type = value.GetType();
    6178      if (obj2id.ContainsKey(value))
    6279        return ReferenceEnumerator(accessor.Name, obj2id[value]);
    63       if (!typeCache.ContainsKey(value.GetType()))
    64         typeCache.Add(value.GetType(), typeCache.Count);
    65       int typeId = typeCache[value.GetType()];
     80      if (!typeCache.ContainsKey(type))
     81        typeCache.Add(type, typeCache.Count);
     82      int typeId = typeCache[type];
    6683      int? id = null;
    67       if (!value.GetType().IsValueType) {
     84      if ( ! type.IsValueType) {
    6885        id = obj2id.Count;
    6986        obj2id.Add(value, (int)id);
    7087      }
    71       IFormatter formatter = configuration.GetFormatter(value.GetType());
     88      IFormatter formatter = configuration.GetFormatter(type);
    7289      if (formatter != null)
    7390        return PrimitiveEnumerator(accessor.Name, typeId, formatter.Format(value), id);
    74       IDecomposer decomposer = configuration.GetDecomposer(value.GetType());
     91      IDecomposer decomposer = configuration.GetDecomposer(type);
    7592      if (decomposer != null)
    7693        return CompositeEnumerator(accessor.Name, decomposer.Decompose(value), id, typeId, decomposer.CreateMetaInfo(value));
Note: See TracChangeset for help on using the changeset viewer.