Skip to content

Commit 0781e02

Browse files
authored
added ObjectComparer public property (#27)
1 parent 7b4680c commit 0781e02

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

SharpSerializer/Serializing/PropertyFactory.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public sealed class PropertyFactory
4646
/// <summary>
4747
/// Contains reference targets.
4848
/// </summary>
49-
private readonly Dictionary<object, ReferenceTargetProperty> _propertyCache =
50-
new Dictionary<object, ReferenceTargetProperty>();
49+
private readonly Dictionary<object, ReferenceTargetProperty> _propertyCache;
5150

5251
/// <summary>
5352
/// It will be incremented as neccessary
@@ -58,8 +57,18 @@ public sealed class PropertyFactory
5857
/// </summary>
5958
/// <param name = "propertyProvider">provides all important properties of the decomposed object</param>
6059
public PropertyFactory(PropertyProvider propertyProvider)
60+
:this (propertyProvider, EqualityComparer<object>.Default)
61+
{
62+
}
63+
64+
/// <summary>
65+
/// </summary>
66+
/// <param name = "propertyProvider">provides all important properties of the decomposed object</param>
67+
/// <param name="objectComparer">A Comparer, used to define objects with the same references</param>
68+
public PropertyFactory(PropertyProvider propertyProvider, IEqualityComparer<object> objectComparer)
6169
{
6270
_propertyProvider = propertyProvider;
71+
_propertyCache = new Dictionary<object, ReferenceTargetProperty>(objectComparer);
6372
}
6473

6574
/// <summary>

SharpSerializer/SharpSerializer.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
using Polenter.Serialization.Deserializing;
3939
using Polenter.Serialization.Serializing;
4040
using System.Runtime.CompilerServices;
41+
using System.Collections.Generic;
4142

4243
namespace Polenter.Serialization
4344
{
@@ -51,6 +52,7 @@ public sealed class SharpSerializer
5152
private PropertyProvider _propertyProvider;
5253
private string _rootName;
5354
private IPropertySerializer _serializer;
55+
private IEqualityComparer<object> _objectComparer;
5456

5557
/// <summary>
5658
/// Standard Constructor. Default is Xml serializing
@@ -136,6 +138,19 @@ public string RootName
136138
set { _rootName = value; }
137139
}
138140

141+
/// <summary>
142+
/// A Comparer, used to define objects with the same references
143+
/// </summary>
144+
public IEqualityComparer<object> ObjectComparer
145+
{
146+
get
147+
{
148+
if (_objectComparer == null) _objectComparer = EqualityComparer<object>.Default;
149+
return _objectComparer;
150+
}
151+
set { _objectComparer = value; }
152+
}
153+
139154
/// <summary>
140155
/// Gets or sets the instance creator for creating objects.
141156
/// </summary>
@@ -255,7 +270,7 @@ public void Serialize(object data, Stream stream)
255270

256271
lock (_syncObj)
257272
{
258-
var factory = new PropertyFactory(PropertyProvider);
273+
var factory = new PropertyFactory(PropertyProvider, ObjectComparer);
259274

260275
Property property = factory.CreateProperty(RootName, data);
261276

0 commit comments

Comments
 (0)