Skip to content

Commit

Permalink
fix EF not support Expression.Default
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaowlert committed Mar 6, 2019
1 parent 4d3a4a7 commit a519826
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression des
var compareNull = Expression.Equal(source, Expression.Constant(null, source.Type));
set = Expression.IfThenElse(
compareNull,
Expression.Assign(result, arg.DestinationType.CreateDefault()),
Expression.Assign(result, arg.DestinationType.CreateDefault(arg.MapType)),
set);
}
}
Expand Down Expand Up @@ -258,7 +258,7 @@ protected Expression CreateInlineExpressionBody(Expression source, CompileArgume
var compareNull = Expression.Equal(source, Expression.Constant(null, source.Type));
exp = Expression.Condition(
compareNull,
exp.Type.CreateDefault(),
exp.Type.CreateDefault(arg.MapType),
exp);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/ClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected override Expression CreateInlineExpression(Expression source, CompileA
var compareNull = Expression.Equal(member.Getter, Expression.Constant(null, member.Getter.Type));
value = Expression.Condition(
compareNull,
member.DestinationMember.Type.CreateDefault(),
member.DestinationMember.Type.CreateDefault(arg.MapType),
value);
}
var bind = Expression.Bind((MemberInfo)member.DestinationMember.Info, value);
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/PrimitiveAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override Expression CreateExpressionBody(Expression source, Expression
{
//src == null ? default(TDestination) : convert(src)
var compareNull = Expression.Equal(source, Expression.Constant(null, sourceType));
convert = Expression.Condition(compareNull, destinationType.CreateDefault(), convert);
convert = Expression.Condition(compareNull, destinationType.CreateDefault(arg.MapType), convert);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Mapster/Adapters/RecordTypeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override Expression CreateInstantiationExpression(Expression source, E
var parameterInfo = (ParameterInfo)member.DestinationMember.Info;
var defaultConst = parameterInfo.IsOptional
? Expression.Constant(parameterInfo.DefaultValue, member.DestinationMember.Type)
: parameterInfo.ParameterType.CreateDefault();
: parameterInfo.ParameterType.CreateDefault(arg.MapType);

Expression getter;
if (member.Getter == null)
Expand All @@ -54,7 +54,7 @@ protected override Expression CreateInstantiationExpression(Expression source, E
}
if (member.SetterCondition != null)
{
var condition = Expression.Not(member.SetterCondition.Apply(source, arg.DestinationType.CreateDefault()));
var condition = Expression.Not(member.SetterCondition.Apply(source, arg.DestinationType.CreateDefault(arg.MapType)));
getter = Expression.Condition(condition, getter, defaultConst);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mapster/Settings/ValueAccessingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static Expression CustomResolverFn(Expression source, IMemberModel desti
if (getter == null)
{
var type = invokes[0].Item2.Type;
getter = type.CreateDefault();
getter = type.CreateDefault(arg.MapType);
}
foreach (var invoke in invokes)
{
Expand Down Expand Up @@ -124,7 +124,7 @@ private static Expression GetDeepFlattening(Expression source, string propertyNa
return ifTrue;
return Expression.Condition(
Expression.Equal(exp, Expression.Constant(null, exp.Type)),
ifTrue.Type.CreateDefault(),
ifTrue.Type.CreateDefault(arg.MapType),
ifTrue);
}
else if (string.Equals(propertyName, sourceMemberName))
Expand Down Expand Up @@ -187,7 +187,7 @@ private static Expression CustomResolverForDictionaryFn(Expression source, IMemb
break;
}
if (lastCondition != null)
getter = Expression.Condition(lastCondition.Apply(source), getter, getter.Type.CreateDefault());
getter = Expression.Condition(lastCondition.Apply(source), getter, getter.Type.CreateDefault(arg.MapType));
return getter;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Utils/ParameterExpressionReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected override Expression VisitParameter(ParameterExpression node)
if (node == _from[i])
{
if (i >= _to.Length)
return node.Type.CreateDefault();
return node.Type.CreateDefault(MapType.Projection);

ReplaceCounts[i]++;
if (node.Type == _to[i].Type)
Expand Down
12 changes: 6 additions & 6 deletions src/Mapster/Utils/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,13 @@ public static bool IsPrimitiveKind(this Type type)
return type == typeof(object) || type.UnwrapNullable().IsConvertible();
}

public static Expression CreateDefault(this Type type)
public static Expression CreateDefault(this Type type, MapType mapType)
{
return type.GetTypeInfo().IsPrimitive
? Expression.Constant(Activator.CreateInstance(type), type)
: type.CanBeNull()
? Expression.Constant(null, type)
: (Expression)Expression.Default(type);
if (type.CanBeNull())
return Expression.Constant(null, type);
if (type.GetTypeInfo().IsPrimitive || mapType == MapType.Projection)
return Expression.Constant(Activator.CreateInstance(type), type);
return Expression.Default(type);
}
}
}

0 comments on commit a519826

Please sign in to comment.