Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Mapster.Tool - PropertySettingBuilder.Map(member, mapFunc, targetPropertyName) method is not working as expected!

未关闭
#1,017 6 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

已经有一个关联 PR 被合并了。

  • #1018 来自 @DocSvartz —— 已关闭,未合并
  • #1019 来自 @DocSvartz —— 已合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp
领域
tooling

调研方向

从 PropertySettingBuilder.Map 开始,使用 Mapster.Tool 10.0.12 重现 UserCodeGenConfig 示例。跟踪 mapFunc 参数如何传递到生成的 UserMapper.AdaptToDto 输出中;当 FullName 是由 FirstName 和 LastName 共同生成,而不是仅由 LastName 生成时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

I'm using -
Mapster 10.0.12
Mapster.Tool 10.0.12
.NET 10.0.400

I have the following entity model -

public class User
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

and I wanted the following DTO model -

public partial class UserDto
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string FullName { get; set; }
    public int Age { get; set; }
}

and a mapper extension method to be generated which would map User.FirstName + " " + User.LastName to UserDto.FullName.

I used the following code -

public class UserCodeGenConfig : ICodeGenerationRegister
{
    public void Register(CodeGenerationConfig config)
    {
        config.AdaptTo("[name]Dto", MapType.Map)
            .ForType<User>(p =>
            {
                p.Ignore(s => s.FirstName);
                p.Map(s => s.LastName, s => $"{s.FirstName} {s.LastName}", "FullName");
            });

        config.GenerateMapper("[name]Mapper")
            .ForType<User>();
    }
}

and used the model and extension generating commands with Mapster.Tool. Following are the generated codes -

public partial class UserDto
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string FullName { get; set; }
    public int Age { get; set; }
}
public static partial class UserMapper
{
    public static UserDto AdaptToDto(this User p1)
    {
        return p1 == null ? null : new UserDto()
        {
            Id = p1.Id,
            Email = p1.Email,
            FullName = p1.LastName,
            Age = p1.Age
        };
    }
}

As you can see, the DTO model is generated as expected, but the mapping code is exactly what it would generate if I had used -

p.Map(s => s.LastName, "FullName");

instead of -

p.Map(s => s.LastName, s => $"{s.FirstName} {s.LastName}", "FullName");

Following is the source code documentation for the Map method in question -

/// <summary>
/// Map a specific property of the source type to a target property using a custom mapping function.
/// </summary>
/// <typeparam name="TReturn">Type of source property.</typeparam>
/// <typeparam name="TReturn2">Type of target property type.</typeparam>
/// <param name="member">A lambda expression that identifies the source property to be mapped.</param>
/// <param name="mapFunc">A lambda expression that defines the custom mapping function.</param>
/// <param name="targetPropertyName">The name of the target property to which the source property should be mapped.</param>
/// <returns></returns>
public PropertySettingBuilder<T> Map<TReturn, TReturn2>(Expression<Func<T, TReturn>> member, Expression<Func<T, TReturn2>> mapFunc, string? targetPropertyName = null);

So, it seems the tool is not respecting the 2nd parameter (Expression<Func<T, TReturn2>> mapFunc) of this Map method.

Is there anything I failed to understand or is this a bug? Is there any other approach to achieve the expected result?

主要语言
C#
星标
5.2k
派生
411
平均合并
3 天 4 小时
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

MapsterMapper/Mapster 的其他 Issue

查看 MapsterMapper/Mapster 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。