Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #672 - AmbiguousMatchException #753

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
63 changes: 63 additions & 0 deletions src/Mapster.Tests/WhenMappingInitProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

namespace Mapster.Tests;

[TestClass]
public class WhenMappingInitProperty
{

#region Tests
/// <summary>
/// From Issue #672
/// https://github.com/MapsterMapper/Mapster/issues/672
/// </summary>
[TestMethod]
public void WhenMappingToHiddenandNewInitFieldDestination()
{
var source = new Source672() { Id = 156};
var c = source.Adapt<BDestination>();
var s = source.Adapt(new BDestination());

((ADestination)c).Id.ShouldBe(156);
s.Id.ShouldBe(156);
}

[TestMethod]
public void WhenMappingToHiddenandNewInitFieldWithConstructUsing()
{
TypeAdapterConfig<Source672, BDestination>.NewConfig().ConstructUsing(_ => new BDestination());


var source = new Source672() { Id = 256 };
var c = source.Adapt<BDestination>();
var s = source.Adapt(new BDestination());

((ADestination)c).Id.ShouldBe(256);
s.Id.ShouldBe(256);
}


#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
}
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/ClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading