public bool TryGetConstructor(Type collectionType, out ICollectionConstructor constructor)
        {
            if (_options.CollectionConstructors.TryGetValue(collectionType, out var config))
            {
                constructor = GetInstance(config, _services);
                return(true);
            }

            if (collectionType.IsArray)
            {
                constructor = _options.ArrayConstructor != null
                    ? GetInstance(_options.ArrayConstructor, _services)
                    : null;

                return(constructor != null);
            }

            if (collectionType.GetTypeInfo().IsGenericType)
            {
                var definition = collectionType.GetGenericTypeDefinition();
                if (_options.CollectionConstructors.TryGetValue(definition, out config))
                {
                    constructor = GetInstance(config, _services);
                    return(true);
                }
            }

            constructor = null;
            return(false);
        }
Example #2
0
        private object CopyCollection(
            ICollectionConstructor sourceCtor, object sourceCollection,
            ICollectionConstructor targetCtor, Type targetCollectionType)
        {
            int count = sourceCtor.GetElementsCount(sourceCollection);

            object[] values = new object[count];
            sourceCtor.Deconstruct(sourceCollection, values);

            var targetElementType = targetCtor.GetElementType(targetCollectionType);
            var targetCollection  = targetCtor.Construct(targetElementType, values);

            return(targetCollection);
        }