最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

Construction构造函数

互联网 admin 0浏览 0评论

Construction构造函数

AutoMapper can map to destination constructors based on source members:

public class Source {
    public int Value { get; set; }
}
public class SourceDto {
    public SourceDto(int value) {
        _value = value;
    }
    private int _value;
    public int Value {
        get { return _value; }
    }
}
Mapper.Initialize(cfg => cfg.CreateMap<Source, SourceDto>());

If the destination constructor parameter names don't match, you can modify them at config time:

public class Source {
    public int Value { get; set; }
}
public class SourceDto {
    public SourceDto(int valueParamSomeOtherName) {
        _value = valueParamSomeOtherName;
    }
    private int _value;
    public int Value {
        get { return _value; }
    }
}
Mapper.Initialize(cfg => 
  cfg.CreateMap<Source, SourceDto>()
    .ForCtorParam("valueParamSomeOtherName", opt => opt.MapFrom(src => src.Value))
);

This works for both LINQ projections and in-memory mapping.

You can also disable constructor mapping :

Mapper.Initialize(cfg => cfg.DisableConstructorMapping());

转载于:.html

Construction构造函数

AutoMapper can map to destination constructors based on source members:

public class Source {
    public int Value { get; set; }
}
public class SourceDto {
    public SourceDto(int value) {
        _value = value;
    }
    private int _value;
    public int Value {
        get { return _value; }
    }
}
Mapper.Initialize(cfg => cfg.CreateMap<Source, SourceDto>());

If the destination constructor parameter names don't match, you can modify them at config time:

public class Source {
    public int Value { get; set; }
}
public class SourceDto {
    public SourceDto(int valueParamSomeOtherName) {
        _value = valueParamSomeOtherName;
    }
    private int _value;
    public int Value {
        get { return _value; }
    }
}
Mapper.Initialize(cfg => 
  cfg.CreateMap<Source, SourceDto>()
    .ForCtorParam("valueParamSomeOtherName", opt => opt.MapFrom(src => src.Value))
);

This works for both LINQ projections and in-memory mapping.

You can also disable constructor mapping :

Mapper.Initialize(cfg => cfg.DisableConstructorMapping());

转载于:.html

发布评论

评论列表 (0)

  1. 暂无评论