merge DUI3/Alpha into sdk (#13)

* merge DUI3/Alpha into sdk

* formatting
This commit is contained in:
Jedd Morgan
2024-07-08 14:03:54 +01:00
committed by GitHub
parent 82b7ca1a26
commit b2883c91e4
@@ -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<object> 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;
}
/// <summary>
/// Tests that the given <paramref name="type"/> is assignable from a generic type def <see cref="List{T}"/>
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
[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<>);
}
}