小编Mig*_*uke的帖子

Automapper:使用ReverseMap()和ForMember()进行双向映射

我有这种情况,我想将实体映射到viewmodel并返回.我必须使用ForMember()显式指定映射,因为它们的属性不共享完全相同的名称.这是我的类看起来如何的简短示例:

public class PartTwo {
    public int Integer { get; set; }
}

public class PartTwoViewModel {
    public int PartInteger { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我想用这种方式使用它们:

Mapper.CreateMap<PartTwo, PartTwoViewModel>()
    .ForMember(dst => dst.PartInteger, opt => opt.MapFrom(src => src.Integer))
    .ReverseMap();

var partTwoViewModel = new PartTwoViewModel() { PartInteger = 42 };
var partTwo = Mapper.Map<PartTwoViewModel, PartTwo>(partTwoViewModel);
Assert.AreEqual(partTwoViewModel.PartInteger, partTwo.Integer);
Run Code Online (Sandbox Code Playgroud)

但它与PartInteger属性不匹配.(整数为0.)

有没有办法让这项工作?(当两个类的属性具有相同的名称时.)我是否必须在方法ForMember()中设置某种选项?

.net bidirectional automapper

58
推荐指数
2
解决办法
4万
查看次数

ILMerge 程序集未正确合并

我正在使用 ILMerge 版本 2.11.1103.0 将 4 个 DLL 合并到一个新程序集中。假设他们分别称为A、B、C、D,那么他们的关系如下:

B -> A
C -> A
D -> A
D -> C
Run Code Online (Sandbox Code Playgroud)

当我在 A、B 和 C 上运行 ILMerge 时,一切都运行得很好。但是当我添加 DI 时收到以下错误:

An exception occurred during merging:
ILMerge.Merge: The assembly 'A' was not merged in correctly.
It is still listed as an external reference in the target assembly.
   at ILMerging.ILMerge.Merge()
   at ILMerging.ILMerge.Main(String[] args)
Run Code Online (Sandbox Code Playgroud)

我已尝试使用 /close 选项,如此处所示ILMerge DLL: Assembly not merged in正确,仍列为外部引用,但不幸的是它没有帮助:

In order to close the target assembly, the number …
Run Code Online (Sandbox Code Playgroud)

c# ilmerge build-process build .net-assembly

5
推荐指数
1
解决办法
7251
查看次数

自动映射:dto对象的自动映射集合属性

我有一个域对象

public class ProductModel
{
    public long Id {get;set;}
    public string Name {get;set;}
    public string SerialNumber {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

单Dto类:

public class ProductDto
{
    public long Id {get;set;}
    public string Name {get;set;}
    public string SerialNumber {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

单个Dto类,它是Dto对象的列表:

public class ProductListDto : List<ProductDto>
{
    public List<ProductDto> Products;

    public ProductListDto()
    {
        Products = new List<ProductDto>();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将域对象列表映射到Dto对象列表,以便ProductListDto对象AUTOMATICALLY的"Products"属性与ProductModel对象列表一起映射:

ProductListDto dto = new ProductListDto();  

Mapper.CreateMap<ProductModel, ProductDto>();

/* dto = (ProductListDto) Mapper.Map<List<ProductModel>, List<ProductDto>>((List<ProductModel>)model);  this code line causes error. It is commented out. */ 

dto.Products …
Run Code Online (Sandbox Code Playgroud)

automapper

5
推荐指数
1
解决办法
1万
查看次数

我可以使用automapper将多个对象映射到目标对象

UserAccount objUserAccount=null;
AutoMapper.Mapper.CreateMap<AccountBO, UserAccount>();
objUserAccount = AutoMapper.Mapper.Map<AccountBO, UserAccount>(lstAcc[0]);
Run Code Online (Sandbox Code Playgroud)

到目前为止,映射AccountBO属性很好.

现在我必须将对象objAddressBO属性映射到目标,包括上面的映射值.为此,我在上面的代码行中编写了如下代码.

AutoMapper.Mapper.CreateMap<AddressBO,UserAccount>();
objUserAccount=AutoMapper.Mapper.Map<AddressBO,UserAccount>(objAddressBO);
Run Code Online (Sandbox Code Playgroud)

但它丢失了第一次映射值并仅返回最后一次映射值.

请让我知道在目标对象中同时包含两个值时需要做些哪些更改.

c# mapping automapper

1
推荐指数
1
解决办法
2742
查看次数