public static async Task BindCollectionAsync()

in extensions/Worker.Extensions.Shared/Reflection/ParameterBinder.cs [32:74]


        public static async Task<object> BindCollectionAsync(Func<Type, IAsyncEnumerable<object>> factory, Type collectionType)
        {
            if (factory is null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (collectionType is null)
            {
                throw new ArgumentNullException(nameof(collectionType));
            }

            if (!collectionType.TryGetCollectionElementType(out Type? elementType))
            {
                throw new ArgumentException($"Type '{collectionType}' is not a collection type.", nameof(collectionType));
            }

            object? collection = null;
            if (collectionType.IsConcreteType() && !collectionType.IsArray)
            {
                collection = Activator.CreateInstance(collectionType)!;
            }
            else if (IsListInterface(collectionType))
            {
                collection = Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType!))!;
            }
            else if (collectionType.IsArray)
            {
                collection = Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType!))!;
                await BindCollectionAsync(factory(elementType!), collection);
                IList list = (IList)collection;
                Array arrayResult = Array.CreateInstance(elementType!, list.Count);
                list.CopyTo(arrayResult, 0);
                return arrayResult;
            }
            else
            {
                throw new ArgumentException($"Collection type '{collectionType}' is not supported for parameter binding.", nameof(collectionType));
            }

            await BindCollectionAsync(factory(elementType!), collection);
            return collection;
        }