Skip to content

Commit 51d5a71

Browse files
committed
fix: refactoring and fix DirectAssignmentForSameType implimentation
1 parent 1aaa504 commit 51d5a71

3 files changed

Lines changed: 13 additions & 23 deletions

File tree

src/Mapster/Adapters/BaseAdapter.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,6 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp
501501

502502
//adapt(_source);
503503
var notUsingDestinationValue = mapping is not { UseDestinationValue: true };
504-
505-
if(_source.Type == destinationType && notUsingDestinationValue
506-
&& arg.IsDirectAssignmentForSameTypeEnable(rule, tuple) && arg.IsNotCustomConverterFactory(rule))
507-
return _source.To(destinationType);
508-
509504
var exp = _source.Type == destinationType && arg.Settings.ShallowCopyForSameType == true && notUsingDestinationValue
510505
&& rule == null
511506
? _source

src/Mapster/Adapters/ClassAdapter.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,5 +291,18 @@ private static Expression SetValueByReflection(MemberMapping member, MemberExpre
291291

292292
return Expression.MemberInit(newInstance, lines);
293293
}
294+
295+
protected override Expression CreateExpressionBody(Expression source, Expression? destination, CompileArgument arg)
296+
{
297+
TypeAdapterRule? rule;
298+
var tuple = new TypeTuple(source.Type, arg.DestinationType);
299+
arg.Context.Config.RuleMap.TryGetValue(tuple, out rule);
300+
301+
if (source.Type == arg.DestinationType && !arg.UseDestinationValue
302+
&& arg.Settings.DirectAssignmentForSameType.GetValueOrDefault() && arg.IsNotCustomConverterFactory(rule))
303+
return source;
304+
305+
return base.CreateExpressionBody(source, destination, arg);
306+
}
294307
}
295308
}

src/Mapster/Utils/ReflectionUtils.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -467,24 +467,6 @@ public static bool IsNotSelfCreation(this Type type)
467467
return type.GetFieldsAndProperties().All(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) == 0);
468468
}
469469

470-
public static bool IsDirectAssignmentForSameTypeEnable(this CompileArgument arg, TypeAdapterRule? rule, TypeTuple tuple)
471-
{
472-
if (rule != null
473-
&& rule.Settings.DirectAssignmentForSameType.HasValue)
474-
return rule.Settings.DirectAssignmentForSameType.GetValueOrDefault();
475-
476-
if (arg.Context.Config.Rules
477-
.Where(x => x.Settings.DestinationType == tuple.Destination)
478-
.Select(x => x.Settings)
479-
.Any(x => x.DirectAssignmentForSameType.HasValue && x.DirectAssignmentForSameType.GetValueOrDefault()))
480-
return true;
481-
482-
if (arg.Context.Config.Default.Settings.DirectAssignmentForSameType.GetValueOrDefault())
483-
return true;
484-
485-
return false;
486-
}
487-
488470
public static bool IsNotCustomConverterFactory(this CompileArgument arg, TypeAdapterRule? rule)
489471
{
490472
if(rule != null)

0 commit comments

Comments
 (0)