From aa496feb60f34e7fab5d16314b2852dd640e998a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 18 Jul 2021 17:41:45 -0500 Subject: make DiffWith nullable friendly --- src/slskd/Common/Extensions.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index eab5b02a..3b52c650 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -37,25 +37,23 @@ namespace slskd /// A list of differences between the two objects. public static IEnumerable<(PropertyInfo Property, string FQN, object Left, object Right)> DiffWith(this object left, object right, string parentFqn = null) { - if (left.GetType() != right.GetType()) + if (left?.GetType() != right?.GetType()) { - throw new InvalidCastException($"Unable to diff types {left.GetType()} and {right.GetType()}"); + throw new InvalidCastException($"Unable to diff types {left?.GetType()} and {right?.GetType()}"); } var differences = new List<(PropertyInfo Property, string FQN, object Left, object Right)>(); - foreach (var prop in left.GetType().GetProperties()) + foreach (var prop in left?.GetType().GetProperties()) { var leftVal = prop.GetValue(left); var rightVal = prop.GetValue(right); var propType = prop.PropertyType; var fqn = string.IsNullOrEmpty(parentFqn) ? prop.Name : string.Join(".", parentFqn, prop.Name); - var quasiPrimitives = new[] { typeof(string), typeof(decimal) }; - - if (propType.IsPrimitive || quasiPrimitives.Contains(propType)) + if (propType.IsPrimitive || Nullable.GetUnderlyingType(propType) != null || new[] { typeof(string), typeof(decimal) }.Contains(propType)) { - if (!leftVal.Equals(rightVal)) + if (!Equals(leftVal, rightVal)) { differences.Add((prop, fqn, leftVal, rightVal)); } -- cgit v1.2.3