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 #656 #748

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 5 additions & 7 deletions src/Mapster/Adapters/ClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected override Expression CreateBlockExpression(Expression source, Expressio
{
var destinationPropertyInfo = (PropertyInfo)member.DestinationMember.Info!;
adapt = destinationPropertyInfo.IsInitOnly()
? SetValueByReflection(destination, (MemberExpression)adapt, arg.DestinationType)
? SetValueByReflection(member, (MemberExpression)adapt)
: member.DestinationMember.SetExpression(destination, adapt);
}
catch (Exception e)
Expand Down Expand Up @@ -178,19 +178,17 @@ protected override Expression CreateBlockExpression(Expression source, Expressio
return lines.Count > 0 ? (Expression)Expression.Block(lines) : Expression.Empty();
}

private static Expression SetValueByReflection(Expression destination, MemberExpression adapt,
Type destinationType)
private static Expression SetValueByReflection(MemberMapping member, MemberExpression adapt)
{
var memberName = adapt.Member.Name;
var typeofExpression = Expression.Constant(destinationType);
var typeofExpression = Expression.Constant(member.Destination!.Type);
var getPropertyMethod = typeof(Type).GetMethod("GetProperty", new[] { typeof(string) })!;
var getPropertyExpression = Expression.Call(typeofExpression, getPropertyMethod,
Expression.Constant(memberName));
Expression.Constant(member.DestinationMember.Name));
var setValueMethod =
typeof(PropertyInfo).GetMethod("SetValue", new[] { typeof(object), typeof(object) })!;
var memberAsObject = adapt.To(typeof(object));
return Expression.Call(getPropertyExpression, setValueMethod,
new[] { destination, memberAsObject });
new[] { member.Destination, memberAsObject });
}

protected override Expression? CreateInlineExpression(Expression source, CompileArgument arg)
Expand Down
Loading