elsa-core/src/modules/Elsa.JavaScript/Helpers/ObjectArrayHelper.cs
Sipke Schoorstra a1c30facba Add ObjectArrayHelper and update array detection method
A new ObjectArrayHelper class has been added into the Elsa.JavaScript module to improve array detection. The previous IsArrayLike method used in JintJavaScriptEvaluator and integration tests has been replaced by the DetermineIfObjectIsArrayLikeClrCollection method from this helper. These changes also provoked an update of the Jint package version from 3.0.0-beta-2057 to 3.0.0.
2024-01-31 21:10:13 +01:00

39 lines
1 KiB
C#

using System.Collections;
namespace Elsa.JavaScript.Helpers;
/// <summary>
/// Contains helper methods for working with object arrays.
/// </summary>
public static class ObjectArrayHelper
{
/// <summary>
/// Determines if the specified object is an array-like CLR collection.
/// </summary>
public static bool DetermineIfObjectIsArrayLikeClrCollection(Type type)
{
var isDictionary = typeof(IDictionary).IsAssignableFrom(type);
if (isDictionary)
return false;
if (typeof(ICollection).IsAssignableFrom(type))
return true;
foreach (var interfaceType in type.GetInterfaces())
{
if (!interfaceType.IsGenericType)
{
continue;
}
if (interfaceType.GetGenericTypeDefinition() == typeof(IReadOnlyCollection<>)
|| interfaceType.GetGenericTypeDefinition() == typeof(ICollection<>))
{
return true;
}
}
return false;
}
}