From b2883c91e485de9f6dc65b4a22bf433b28585fd7 Mon Sep 17 00:00:00 2001 From: Jedd Morgan <45512892+JR-Morgan@users.noreply.github.com> Date: Mon, 8 Jul 2024 14:03:54 +0100 Subject: [PATCH] merge DUI3/Alpha into sdk (#13) * merge DUI3/Alpha into sdk * formatting --- .../SerializationUtilities/ValueConverter.cs | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Speckle.Core/Serialisation/SerializationUtilities/ValueConverter.cs b/src/Speckle.Core/Serialisation/SerializationUtilities/ValueConverter.cs index b203a976..58fa5ecb 100644 --- a/src/Speckle.Core/Serialisation/SerializationUtilities/ValueConverter.cs +++ b/src/Speckle.Core/Serialisation/SerializationUtilities/ValueConverter.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.Drawing; using System.Globalization; using Speckle.Core.Logging; @@ -157,16 +158,17 @@ internal static class ValueConverter #endregion } - // Handle List - if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>)) + // Handle List<>, IList<>, and IReadOnlyList<> + if (type.IsGenericType && IsGenericList(type)) { if (value is not List valueList) { return false; } + var targetType = typeof(List<>).MakeGenericType(type.GenericTypeArguments); Type listElementType = type.GenericTypeArguments[0]; - IList ret = (IList)Activator.CreateInstance(type, valueList.Count); + IList ret = (IList)Activator.CreateInstance(targetType, valueList.Count); foreach (object inputListElement in valueList) { if (!ConvertValue(listElementType, inputListElement, out object? convertedListElement)) @@ -311,4 +313,21 @@ internal static class ValueConverter return false; } + + /// + /// Tests that the given is assignable from a generic type def + /// + /// + /// + [Pure] + private static bool IsGenericList(Type type) + { + if (!type.IsGenericType) + { + return false; + } + + Type typeDef = type.GetGenericTypeDefinition(); + return typeDef == typeof(List<>) || typeDef == typeof(IList<>) || typeDef == typeof(IReadOnlyList<>); + } }