Skip to content

Commit

Permalink
merge DUI3/Alpha into sdk (#13)
Browse files Browse the repository at this point in the history
* merge DUI3/Alpha into sdk

* formatting
  • Loading branch information
JR-Morgan authored Jul 8, 2024
1 parent 82b7ca1 commit b2883c9
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -157,16 +158,17 @@ public static bool ConvertValue(Type type, object? value, out object? convertedV
#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))
Expand Down Expand Up @@ -311,4 +313,21 @@ public static bool ConvertValue(Type type, object? value, out object? convertedV

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<>);
}
}

0 comments on commit b2883c9

Please sign in to comment.