Skip to content

Commit

Permalink
MapsterMapper#524 Object To TDestination fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
DocSvartz committed Oct 18, 2023
1 parent f678d12 commit 6ef1fe0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/Mapster.Tests/WhenMappingRecordRegression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@ public void DetectFakeRecord()
object.ReferenceEquals(_destination, _result).ShouldBeTrue();
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/524
/// </summary>
[TestMethod]
public void TSousreIsObjectUpdate()
{
var source = new TestClassPublicCtr { X = 123 };
var _result = Somemap(source);

_result.X.ShouldBe(123);
}

TestClassPublicCtr Somemap(object source)
{
var dest = new TestClassPublicCtr { X = 321 };
var dest1 = source.Adapt(dest);

return dest;
}

#region NowNotWorking

/// <summary>
Expand All @@ -268,29 +288,6 @@ public void CollectionUpdate()
destination.Count.ShouldBe(_result.Count);
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/524
/// Not work. Already has a special overload:
/// .Adapt(this object source, object destination, Type sourceType, Type destinationType)
/// </summary>
[Ignore]
[TestMethod]
public void TSousreIsObjectUpdate()
{
var source = new TestClassPublicCtr { X = 123 };
var _result = Somemap(source);

_result.X.ShouldBe(123);
}

TestClassPublicCtr Somemap(object source)
{
var dest = new TestClassPublicCtr { X = 321 };
var dest1 = source.Adapt(dest); // typeof(TSource) always return Type as Object. Need use dynamic or Cast to Runtime Type before Adapt

return dest;
}

#endregion NowNotWorking

}
Expand Down
13 changes: 13 additions & 0 deletions src/Mapster/Adapters/ObjectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,18 @@ protected override Expression CreateInlineExpression(Expression source, CompileA
{
return CreateInstantiationExpression(source, arg);
}

protected override Expression CreateExpressionBody(Expression source, Expression? destination, CompileArgument arg)
{

if(destination == null || arg.DestinationType == typeof(object))
return base.CreateExpressionBody(source, destination, arg);
else
{
var convert = arg.Context.Config.CreateDynamicMapInvokeExpressionBody(arg.DestinationType, source);
return arg.Context.Config.CreateMapToTargetInvokeExpressionBody(convert.Type, destination.Type, convert, destination);
}

}
}
}

0 comments on commit 6ef1fe0

Please sign in to comment.