Skip to content

Commit 218163e

Browse files
committed
Fixed: Trying to obtain differences from a property whose type is an specilization of Dictionary raises an exception.
1 parent 401863b commit 218163e

3 files changed

Lines changed: 220 additions & 96 deletions

File tree

src/ObjDiff/ObjDiff.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,23 @@ private static IEnumerable<Difference> Diff<T>(T left, T right, CompareOptions c
162162

163163
if (IsCollection(property))
164164
{
165-
var collectionType = property.PropertyType.IsArray ? property.PropertyType.GetElementType() : property.PropertyType.GetGenericArguments()[0];
165+
Type collectionType = null;
166+
167+
if (property.PropertyType.IsArray)
168+
collectionType = property.PropertyType.GetElementType();
169+
170+
else
171+
{
172+
var genericArguments = property.PropertyType.GetGenericArguments();
173+
174+
while (genericArguments != null && genericArguments.Length == 0)
175+
genericArguments = property.PropertyType.BaseType?.GetGenericArguments();
176+
177+
if (genericArguments == null)
178+
throw new Exception("Unable to obtain generic arguments of a collection property");
179+
180+
collectionType = genericArguments[0];
181+
}
166182

167183
if (!HasEqualityDefined(collectionType))
168184
continue;
@@ -277,7 +293,9 @@ private static bool IsCollectionItem(string indexedProperty, out string property
277293

278294
private static bool HasEqualityDefined(Type type)
279295
{
280-
if (type.GetInterface(typeof(IEquatable<>).Name) != null)
296+
var filter = new TypeFilter((c, o) => c.ToString() == o.ToString());
297+
298+
if (type.FindInterfaces(filter, typeof(IEquatable<>)).Length > 0)
281299
return true;
282300

283301
return type.GetMethod("Equals", new Type[] { typeof(object) }).DeclaringType != typeof(object);
@@ -312,6 +330,12 @@ bool IsNullableSimpleType(Type t)
312330
}
313331

314332

333+
private static bool IsDictionary(PropertyInfo propertyInfo)
334+
{
335+
return propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>);
336+
}
337+
338+
315339
private static bool IsCollection(PropertyInfo propertyInfo)
316340
{
317341
return propertyInfo.PropertyType != typeof(string) &&

src/ObjDiff/ObjDiff.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<Authors>Israel Gómez de Celis González</Authors>
1313
<Description>A C# .NET Standard library that allows to obtain the differences between two objects and, optionally, patch the first object with these differences so it becomes equal to the second one.</Description>
1414
<Copyright>Copyright (c) 2021 Israel Gómez de Celis González</Copyright>
15-
<Version>1.3.1</Version>
16-
<PackageReleaseNotes>Added support for .NET 6 DateOnly and TimeOnly structures.</PackageReleaseNotes>
15+
<Version>1.3.2</Version>
16+
<PackageReleaseNotes>Bug fixes.</PackageReleaseNotes>
1717
</PropertyGroup>
1818

1919
<ItemGroup>

0 commit comments

Comments
 (0)