小编Thi*_*ino的帖子

AutoMapper - 如何将参数传递给 Profile?

我想使用 automapper 在我的公共数据合同和我的 BD 模型之间进行映射。我需要将一个字符串参数传递到我的 MapProfile 并从我的属性中获取描述(在本例中为“代码”)。例如:

public class Source
{
    public int Code { get; set; }
}

public class Destination
{
    public string Description { get; set; }
}

public class Dic
{
    public static string GetDescription(int code, string tag)
    {
            //do something
            return "My Description";
    }
}

public class MyProfile : Profile
{

    protected override void Configure()
    {
        CreateMap<Destination, Source>()
            .ForMember(dest => dest.Description, 
                opt => /* something */ 
                Dic.GetDescription(code, tag));
    }
}

public class MyTest 
{

        [Fact] …
Run Code Online (Sandbox Code Playgroud)

c# automapper

3
推荐指数
2
解决办法
5865
查看次数

标签 统计

automapper ×1

c# ×1