diff --git a/src/Mapster.Tests/WhenMappingWithDictionary.cs b/src/Mapster.Tests/WhenMappingWithDictionary.cs index df38030b..8b53a3f6 100644 --- a/src/Mapster.Tests/WhenMappingWithDictionary.cs +++ b/src/Mapster.Tests/WhenMappingWithDictionary.cs @@ -37,7 +37,6 @@ public void Object_To_Dictionary() dict["Name"].ShouldBe(poco.Name); } - [TestMethod] public void Object_To_Dictionary_Map() { @@ -57,6 +56,25 @@ public void Object_To_Dictionary_Map() dict["Name"].ShouldBe(poco.Name); } + [TestMethod] + public void Object_To_Dictionary_Map_With_Periods() + { + var poco = new SimplePoco + { + Id = Guid.NewGuid(), + Name = "test", + }; + + var config = new TypeAdapterConfig(); + config.NewConfig>() + .Map("Key.With.Periods", c => c.Id); + var dict = poco.Adapt>(config); + + dict.Count.ShouldBe(2); + dict["Key.With.Periods"].ShouldBe(poco.Id); + dict["Name"].ShouldBe(poco.Name); + } + [TestMethod] public void Object_To_Dictionary_CamelCase() { @@ -154,6 +172,23 @@ public void Dictionary_To_Object_Map() poco.Name.ShouldBeNull(); } + [TestMethod] + public void Dictionary_To_Object_Map_With_Periods() + { + var dict = new Dictionary + { + ["Key.With.Periods"] = Guid.NewGuid(), + ["Foo"] = "test", + }; + + TypeAdapterConfig, SimplePoco>.NewConfig() + .Map(c => c.Id, "Key.With.Periods"); + + var poco = TypeAdapter.Adapt(dict); + poco.Id.ShouldBe(dict["Key.With.Periods"]); + poco.Name.ShouldBeNull(); + } + [TestMethod] public void Dictionary_To_Object_CamelCase() { diff --git a/src/Mapster/Adapters/DictionaryAdapter.cs b/src/Mapster/Adapters/DictionaryAdapter.cs index 25af8e3b..f81249b4 100644 --- a/src/Mapster/Adapters/DictionaryAdapter.cs +++ b/src/Mapster/Adapters/DictionaryAdapter.cs @@ -219,7 +219,7 @@ protected override Expression CreateInlineExpression(Expression source, CompileA protected override ClassModel GetSetterModel(CompileArgument arg) { //get member name from map - var destNames = arg.GetDestinationNames().AsEnumerable(); + var destNames = arg.GetDestinationNames(false).AsEnumerable(); //get member name from properties if (arg.SourceType.GetDictionaryType() == null) diff --git a/src/Mapster/Compile/CompileArgument.cs b/src/Mapster/Compile/CompileArgument.cs index 2e15637a..5dd90b4b 100644 --- a/src/Mapster/Compile/CompileArgument.cs +++ b/src/Mapster/Compile/CompileArgument.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; -using Mapster.Utils; namespace Mapster { @@ -20,16 +19,18 @@ public class CompileArgument internal HashSet GetSourceNames() { return _srcNames ??= (from it in Settings.Resolvers - where it.SourceMemberName != null - select it.SourceMemberName!.Split('.').First()).ToHashSet(); + where it.SourceMemberName != null + select it.SourceMemberName!.Split('.').First()).ToHashSet(); } private HashSet? _destNames; - internal HashSet GetDestinationNames() + internal HashSet GetDestinationNames(bool split = true) { return _destNames ??= (from it in Settings.Resolvers - where it.DestinationMemberName != null - select it.DestinationMemberName.Split('.').First()).ToHashSet(); + where it.DestinationMemberName != null + select split + ? it.DestinationMemberName.Split('.').First() + : it.DestinationMemberName).ToHashSet(); } private bool _fetchConstructUsing;