- Timestamp:
- 04/29/09 13:52:20 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r1679 r1701 7 7 using HeuristicLab.Persistence.Default.Decomposers.Storable; 8 8 using System.Text; 9 using System.Runtime.InteropServices; 9 10 10 11 namespace 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 } 11 27 12 28 public class Serializer : IEnumerable<ISerializationToken> { … … 43 59 this.rootName = rootName; 44 60 this.configuration = configuration; 45 obj2id = new Dictionary<object, int>{ { new object(), 0 } };61 obj2id = new Dictionary<object,int>(new ReferenceEqualityComparer()) { { new object(), 0 } }; 46 62 typeCache = new Dictionary<Type, int>(); 47 63 } … … 59 75 if (value == null) 60 76 return NullReferenceEnumerator(accessor.Name); 77 Type type = value.GetType(); 61 78 if (obj2id.ContainsKey(value)) 62 79 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]; 66 83 int? id = null; 67 if ( !value.GetType().IsValueType) {84 if ( ! type.IsValueType) { 68 85 id = obj2id.Count; 69 86 obj2id.Add(value, (int)id); 70 87 } 71 IFormatter formatter = configuration.GetFormatter( value.GetType());88 IFormatter formatter = configuration.GetFormatter(type); 72 89 if (formatter != null) 73 90 return PrimitiveEnumerator(accessor.Name, typeId, formatter.Format(value), id); 74 IDecomposer decomposer = configuration.GetDecomposer( value.GetType());91 IDecomposer decomposer = configuration.GetDecomposer(type); 75 92 if (decomposer != null) 76 93 return CompositeEnumerator(accessor.Name, decomposer.Decompose(value), id, typeId, decomposer.CreateMetaInfo(value));
Note: See TracChangeset
for help on using the changeset viewer.