From cea3debb0267343f231a2f1179b808be2e24526d Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Sat, 4 Jan 2025 19:41:14 +0500 Subject: [PATCH 1/3] Fix issue #672 --- src/Mapster/Adapters/ClassAdapter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mapster/Adapters/ClassAdapter.cs b/src/Mapster/Adapters/ClassAdapter.cs index adf1c60c..407c5a5b 100644 --- a/src/Mapster/Adapters/ClassAdapter.cs +++ b/src/Mapster/Adapters/ClassAdapter.cs @@ -183,7 +183,7 @@ private static Expression SetValueByReflection(MemberMapping member, MemberExpre var typeofExpression = Expression.Constant(member.Destination!.Type); var getPropertyMethod = typeof(Type).GetMethod("GetProperty", new[] { typeof(string) })!; var getPropertyExpression = Expression.Call(typeofExpression, getPropertyMethod, - Expression.Constant(member.DestinationMember.Name)); + Expression.Constant(member.DestinationMember.Name, member.DestinationMember.Type)); var setValueMethod = typeof(PropertyInfo).GetMethod("SetValue", new[] { typeof(object), typeof(object) })!; var memberAsObject = adapt.To(typeof(object)); From 4deea1a08701d2741ecbf0f366196204dc33e2c9 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Sat, 4 Jan 2025 20:15:20 +0500 Subject: [PATCH 2/3] add mapping init PropertyTest --- src/Mapster.Tests/WhenMappingInitProperty.cs | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/Mapster.Tests/WhenMappingInitProperty.cs diff --git a/src/Mapster.Tests/WhenMappingInitProperty.cs b/src/Mapster.Tests/WhenMappingInitProperty.cs new file mode 100644 index 00000000..469fb9f8 --- /dev/null +++ b/src/Mapster.Tests/WhenMappingInitProperty.cs @@ -0,0 +1,48 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; + +namespace Mapster.Tests; + +[TestClass] +public class WhenMappingInitProperty +{ + + #region Tests + /// + /// From Issue #672 + /// https://github.com/MapsterMapper/Mapster/issues/672 + /// + [TestMethod] + public void WhenMappingToHiddenandNewInitFieldDestination() + { + var source = new Source672() { Id = 156}; + var c = source.Adapt(); + var s = source.Adapt(new BDestination()); + + ((ADestination)c).Id.ShouldBe(156); + s.Id.ShouldBe(156); + } + + #endregion Tests + + + #region TestClasses + + class Source672 + { + public long Id { get; init; } + } + + class ADestination + { + public int Id { get; init; } + } + + class BDestination : ADestination + { + public new long Id { get; init; } + } + + + #endregion TestClasses +} From a8baff8d2a13f3ab319012b2aec1369e846a3af6 Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Sat, 4 Jan 2025 20:38:01 +0500 Subject: [PATCH 3/3] add Constract Using Test --- src/Mapster.Tests/WhenMappingInitProperty.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Mapster.Tests/WhenMappingInitProperty.cs b/src/Mapster.Tests/WhenMappingInitProperty.cs index 469fb9f8..4fda1295 100644 --- a/src/Mapster.Tests/WhenMappingInitProperty.cs +++ b/src/Mapster.Tests/WhenMappingInitProperty.cs @@ -23,6 +23,21 @@ public void WhenMappingToHiddenandNewInitFieldDestination() s.Id.ShouldBe(156); } + [TestMethod] + public void WhenMappingToHiddenandNewInitFieldWithConstructUsing() + { + TypeAdapterConfig.NewConfig().ConstructUsing(_ => new BDestination()); + + + var source = new Source672() { Id = 256 }; + var c = source.Adapt(); + var s = source.Adapt(new BDestination()); + + ((ADestination)c).Id.ShouldBe(256); + s.Id.ShouldBe(256); + } + + #endregion Tests