Skip to content

Commit 2e3b235

Browse files
committed
fix #195 map to target using struct
1 parent ac3cf6b commit 2e3b235

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Shouldly;
3+
4+
namespace Mapster.Tests
5+
{
6+
[TestClass]
7+
public class WhenMappingStruct
8+
{
9+
class MyClassA
10+
{
11+
public MyStruct MyStruct { get; set; }
12+
}
13+
class MyClassB
14+
{
15+
public MyStruct MyStruct { get; set; }
16+
}
17+
18+
struct MyStruct
19+
{
20+
public MyStruct(string prop) : this()
21+
{
22+
Property = prop;
23+
}
24+
25+
public string Property { get; set; }
26+
}
27+
28+
[TestMethod]
29+
public void TestMapping()
30+
{
31+
var a = new MyClassA();
32+
33+
a.MyStruct = new MyStruct("A");
34+
35+
var b = a.Adapt<MyClassB>();
36+
37+
b.MyStruct.Property.ShouldBe("A");
38+
}
39+
40+
[TestMethod]
41+
public void TestMappingToTarget()
42+
{
43+
var a = new MyClassA();
44+
var b = new MyClassB();
45+
46+
a.MyStruct = new MyStruct("A");
47+
48+
a.Adapt(b);
49+
50+
b.MyStruct.Property.ShouldBe("A");
51+
}
52+
}
53+
}

src/Mapster/Adapters/BaseAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de
168168

169169
//new TDest();
170170
var set = CreateInstantiationExpression(source, destination, arg);
171-
if (destination != null && this.UseTargetValue && arg.GetConstructUsing()?.Parameters.Count != 2)
171+
if (destination != null && this.UseTargetValue && arg.GetConstructUsing()?.Parameters.Count != 2 && destination.CanBeNull())
172172
{
173173
//dest ?? new TDest();
174174
set = Expression.Coalesce(destination, set);

src/Mapster/Mapster.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.1</NetStandardImplicitPackageVersion>
1818
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1919
<RootNamespace>Mapster</RootNamespace>
20-
<Version>4.0.0</Version>
21-
<AssemblyVersion>4.0.0.0</AssemblyVersion>
20+
<Version>4.1.0</Version>
21+
<AssemblyVersion>4.1.0.0</AssemblyVersion>
2222
<LangVersion>preview</LangVersion>
2323
<NullableContextOptions>enable</NullableContextOptions>
2424
</PropertyGroup>

0 commit comments

Comments
 (0)