diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 3efb4b24..199c084e 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ false - 10.0.5 + 10.0.6 netstandard2.0;net10.0;net9.0;net8.0 netstandard2.0;net10.0;net9.0;net8.0 net10.0;net9.0;net8.0 diff --git a/src/Mapster.Core/Enums/MapType.cs b/src/Mapster.Core/Enums/MapType.cs index 420cc812..fa8762f1 100644 --- a/src/Mapster.Core/Enums/MapType.cs +++ b/src/Mapster.Core/Enums/MapType.cs @@ -8,5 +8,6 @@ public enum MapType Map = 1, MapToTarget = 2, Projection = 4, + ApplyNullPropagation = 8, } } \ No newline at end of file diff --git a/src/Mapster.Tests/WhenMappingRecordRegression.cs b/src/Mapster.Tests/WhenMappingRecordRegression.cs index c1bb9dad..85d291e8 100644 --- a/src/Mapster.Tests/WhenMappingRecordRegression.cs +++ b/src/Mapster.Tests/WhenMappingRecordRegression.cs @@ -449,11 +449,7 @@ public void RequiredProperty() { var source = new Person553 { FirstMidName = "John", LastName = "Dow" }; var destination = new Person554 { ID = 245, FirstMidName = "Mary", LastName = "Dow" }; - - TypeAdapterConfig.NewConfig() - //.Map(dest => dest.ID, source => 0) - .Ignore(x => x.ID); - + var s = source.BuildAdapter().CreateMapToTargetExpression(); var result = source.Adapt(destination); diff --git a/src/Mapster/Adapters/BaseAdapter.cs b/src/Mapster/Adapters/BaseAdapter.cs index b519a334..65c9d0f9 100644 --- a/src/Mapster/Adapters/BaseAdapter.cs +++ b/src/Mapster/Adapters/BaseAdapter.cs @@ -220,7 +220,7 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de .Any(y => y.GetType().FullName == "System.Runtime.CompilerServices.RequiredMemberAttribute")); if (requiremembers.Count() != 0) - set = CreateInlineExpression(source, arg, true); + set = CreateInlineExpression(source, arg.CloneWith(MapType.ApplyNullPropagation), true); else set = CreateInstantiationExpression(transformedSource, destination, arg); diff --git a/src/Mapster/Adapters/BaseClassAdapter.cs b/src/Mapster/Adapters/BaseClassAdapter.cs index e46c287a..f092c3ab 100644 --- a/src/Mapster/Adapters/BaseClassAdapter.cs +++ b/src/Mapster/Adapters/BaseClassAdapter.cs @@ -113,7 +113,8 @@ select fn(src, destinationMember, arg)) Destination = (ParameterExpression?)destination, UseDestinationValue = IsCanUsingDestinationValue(arg, destinationMember), }; - if(getter == null && !arg.DestinationType.IsRecordType() + if(arg.MapType == MapType.ApplyNullPropagation && + getter == null && !arg.DestinationType.IsRecordType() && destinationMember.Info is PropertyInfo propinfo) { if (propinfo.GetCustomAttributes() diff --git a/src/Mapster/Compile/CompileArgument.cs b/src/Mapster/Compile/CompileArgument.cs index d324661d..827c1d13 100644 --- a/src/Mapster/Compile/CompileArgument.cs +++ b/src/Mapster/Compile/CompileArgument.cs @@ -48,5 +48,29 @@ select split _fetchConstructUsing = true; return _constructUsing; } + + public CompileArgument CloneWith(MapType? mapType = null) + { + var result = new CompileArgument() + { + SourceType = this.SourceType, + DestinationType = this.DestinationType, + MapType = this.MapType, + ExplicitMapping = this.ExplicitMapping, + Settings = this.Settings, + Context = this.Context, + UseDestinationValue = this.UseDestinationValue, + ConstructorMapping = this.ConstructorMapping, + _srcNames = this._srcNames, + _destNames = this._destNames, + _fetchConstructUsing = this._fetchConstructUsing, + _constructUsing = this._constructUsing + }; + + if (mapType != null) + result.MapType = mapType.Value; + + return result; + } } }