diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilder/DictionaryMappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilder/DictionaryMappingBuilder.cs index 36b68bde60..1841fd1a0e 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBuilder/DictionaryMappingBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBuilder/DictionaryMappingBuilder.cs @@ -25,7 +25,7 @@ public static class DictionaryMappingBuilder if (valueMapping == null) return null; - // target is of type IDictionary<,> or IReadOnlyDictionary<,>. + // target is of type IDictionary<,>, IReadOnlyDictionary<,> or Dictionary<,>. // The constructed type should be Dictionary<,> if (IsDictionaryType(ctx, ctx.Target)) { diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilder/EnumerableMappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilder/EnumerableMappingBuilder.cs index 0b4ad410e2..2de7935a13 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBuilder/EnumerableMappingBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBuilder/EnumerableMappingBuilder.cs @@ -106,10 +106,12 @@ private static (bool CanMapWithLinq, string? CollectMethod) ResolveCollectMethod if (targetIsReadOnlyCollection && sourceCountIsKnown) return (true, ToArrayMethodName); - // if target is a list, ICollection or IReadOnlyCollection collect with ToList() + // if target is a IReadOnlyCollection, IList, List or ICollection with ToList() return targetIsReadOnlyCollection + || SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.IReadOnlyList) + || SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.IList) + || SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.List) || SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.ICollection) - || targetIsReadOnlyCollection ? (true, ToListMethodName) : (false, null); } diff --git a/src/Riok.Mapperly/Descriptors/WellKnownTypes.cs b/src/Riok.Mapperly/Descriptors/WellKnownTypes.cs index 33ec6d531c..4c27d30790 100644 --- a/src/Riok.Mapperly/Descriptors/WellKnownTypes.cs +++ b/src/Riok.Mapperly/Descriptors/WellKnownTypes.cs @@ -24,6 +24,9 @@ public class WellKnownTypes private INamedTypeSymbol? _enumerable; private INamedTypeSymbol? _iCollection; private INamedTypeSymbol? _iReadOnlyCollection; + private INamedTypeSymbol? _iList; + private INamedTypeSymbol? _list; + private INamedTypeSymbol? _iReadOnlyList; private INamedTypeSymbol? _keyValuePair; private INamedTypeSymbol? _dictionary; @@ -44,6 +47,9 @@ internal WellKnownTypes(Compilation compilation) public INamedTypeSymbol Enumerable => _enumerable ??= GetTypeSymbol(typeof(Enumerable)); public INamedTypeSymbol ICollection => _iCollection ??= GetTypeSymbol(typeof(ICollection<>)); public INamedTypeSymbol IReadOnlyCollection => _iReadOnlyCollection ??= GetTypeSymbol(typeof(IReadOnlyCollection<>)); + public INamedTypeSymbol IList => _iList ??= GetTypeSymbol(typeof(IList<>)); + public INamedTypeSymbol List => _list ??= GetTypeSymbol(typeof(List<>)); + public INamedTypeSymbol IReadOnlyList => _iReadOnlyList ??= GetTypeSymbol(typeof(IReadOnlyList<>)); public INamedTypeSymbol KeyValuePair => _keyValuePair ??= GetTypeSymbol(typeof(KeyValuePair<,>)); public INamedTypeSymbol Dictionary => _dictionary ??= GetTypeSymbol(typeof(Dictionary<,>)); diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs index f3e6646970..1f7c6298f7 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs @@ -389,6 +389,39 @@ public void EnumerableToReadOnlyCollectionOfCastedTypes() .HaveSingleMethodBody("return System.Linq.Enumerable.ToList(System.Linq.Enumerable.Select(source, x => (int)x));"); } + [Fact] + public void EnumerableToIListOfCastedTypes() + { + var source = TestSourceBuilder.Mapping( + "IEnumerable", + "IList"); + TestHelper.GenerateMapper(source) + .Should() + .HaveSingleMethodBody("return System.Linq.Enumerable.ToList(System.Linq.Enumerable.Select(source, x => (int)x));"); + } + + [Fact] + public void EnumerableToListOfCastedTypes() + { + var source = TestSourceBuilder.Mapping( + "IEnumerable", + "List"); + TestHelper.GenerateMapper(source) + .Should() + .HaveSingleMethodBody("return System.Linq.Enumerable.ToList(System.Linq.Enumerable.Select(source, x => (int)x));"); + } + + [Fact] + public void EnumerableToIReadOnlyListOfCastedTypes() + { + var source = TestSourceBuilder.Mapping( + "IEnumerable", + "IReadOnlyList"); + TestHelper.GenerateMapper(source) + .Should() + .HaveSingleMethodBody("return System.Linq.Enumerable.ToList(System.Linq.Enumerable.Select(source, x => (int)x));"); + } + [Fact] public void EnumerableToCustomCollection() {