Changeset 1701
- Timestamp:
- 04/29/09 13:52:20 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence
- Files:
-
- 2 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)); -
trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs
r1684 r1701 36 36 [Storable] 37 37 private ulong _ulong = 123456; 38 } 39 40 public class IntWrapper { 41 42 [Storable] 43 public int Value; 44 45 private IntWrapper() { } 46 47 public IntWrapper(int value) { 48 this.Value = value; 49 } 50 51 public override bool Equals(object obj) { 52 if (obj as IntWrapper == null) 53 return false; 54 return Value.Equals(((IntWrapper)obj).Value); 55 } 56 public override int GetHashCode() { 57 return Value.GetHashCode(); 58 } 59 38 60 } 39 61 … … 449 471 } 450 472 473 [TestMethod] 474 public void TestAliasingWithOverriddenEquals() { 475 List<IntWrapper> ints = new List<IntWrapper>(); 476 ints.Add(new IntWrapper(1)); 477 ints.Add(new IntWrapper(1)); 478 Assert.AreEqual(ints[0], ints[1]); 479 Assert.AreNotSame(ints[0], ints[1]); 480 XmlGenerator.Serialize(ints, tempFile); 481 List<IntWrapper> newInts = (List<IntWrapper>)XmlParser.DeSerialize(tempFile); 482 Assert.AreEqual(newInts[0].Value, 1); 483 Assert.AreEqual(newInts[1].Value, 1); 484 Assert.AreEqual(newInts[0], newInts[1]); 485 Assert.AreNotSame(newInts[0], newInts[1]); 486 } 451 487 452 488 [ClassInitialize]
Note: See TracChangeset
for help on using the changeset viewer.