Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Mapster.Tests/WhenMappingCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,19 @@ public void TestEnumList()
}
}

[TestMethod]
public void RespectConstructUsing()
{
var source = new List<int> { 1, 1, 3, };

var result = source
.BuildAdapter()
.ForkConfig(x => x.ForDestinationType<ICollection<string>>().ConstructUsing(() => new HashSet<string>()))
.AdaptToType<ICollection<string>>();

result.ShouldBe(new List<string> { "1", "3", });
}

#region TestClass

[Flags]
Expand Down
7 changes: 7 additions & 0 deletions src/Mapster/Adapters/CollectionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ protected override bool CanInline(Expression source, Expression? destination, Co

protected override Expression CreateInstantiationExpression(Expression source, Expression? destination, CompileArgument arg)
{
//if there is constructUsing, use constructUsing
var constructUsing = arg.GetConstructUsing();
if (constructUsing != null)
{
return base.CreateInstantiationExpression(source, destination, arg);
}

var listType = arg.DestinationType;
if (arg.DestinationType.GetTypeInfo().IsInterface)
{
Expand Down