我使用AutoMapper映射两个对象,但它不映射int
的列表。
这是我正在做的事情:
Public class A
{
public int a{get;set;}
public string b{get;set;}
public List<int> ints{get;set;}
}
Public class B
{
public int a{get;set;}
public string b{get;set;}
public List<int> ints{get;set;}
}
public class MappingService
{
public object MapBToA(B b)
{
Mapper.CreateMap<B,A>();
return Mapper.Map<B,A>(b);
}
}
当我们调用映射函数时,它返回的是object
类型,但是int总是空的。
真实课程的问题
public class SearchRawInput
{
public SearchRawInput()
{
PageNo = 1;
}
public string City { get; set; }
public string Segment1 { get; set; }
public string Segment2 { get; set; }
public TargetAgeGroup? TargetAge { get; set; }
public List<int> CategoryIds { get; set; }
public List<int> SubCategoryIds { get; set; }
public List<int> LocationIds { get; set; }
public List<int> LocationGroupIds { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public decimal? StartPrice { get; set; }
public decimal? EndPrice { get; set; }
public string SearchQuery { get; set; }
public int? PageNo { get; set; }
}
public class ClassSearchInputViewModel
{
public ClassSearchInputViewModel()
{
PageSize = 12;
}
public UrlSeoProcessingResult UrlSeoProcessingResult { get; set; }
public TargetAgeGroup TargetAge { get; set; }
public List<int> CategoryIds { get; set; }
public List<int> SubCategoryIds { get; set; }
public List<int> LocationIds { get; set; }
public List<int> LocationGroupIds { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public decimal? StartPrice { get; set; }
public decimal? EndPrice { get; set; }
public ClassDifficultyLevel DifficultyLevel { get; set; }
public string SearchQuery { get; set; }
public int PageNo { get; set; }
public int PageSize { get; set; }
}
public class SegmentToUrlSeoProcessingResultResolver : ValueResolver<SearchRawInput, UrlSeoProcessingResult>
{
protected override UrlSeoProcessingResult ResolveCore(SearchRawInput source)
{
var processor =new UrlProcessor();
if (source.Segment1 != null && source.Segment2 != null)
{
return processor.Process( (source.Segment1.Trim() + "/" + source.Segment2.Trim()).Trim(), source.City);
}
else if (source.Segment1 == null && source.Segment2 == null)
{
return processor.Process("", source.City);
}
else
{
if (source.Segment1 != null)
{
return processor.Process( (source.Segment1.Trim()).Trim(),source.City);
}
else
{
return processor.Process( source.Segment2.Trim(),source.City);
}
}
}
}
public ClassSearchInputViewModel ClassSearchRawInputToClassSearchInput(SearchRawInput classSearchRawInput)
{
var classSearchInput = Mapper.Map<SearchRawInput, ClassSearchInputViewModel>(classSearchRawInput);
return classSearchInput;
}
Mapper.CreateMap<SearchRawInput,ClassSearchInputViewModel>()
.ForMember(dest => dest.UrlSeoProcessingResult, opt => opt.ResolveUsing<SegmentToUrlSeoProcessingResultResolver>());
上面是完整的代码,当我调用ClassSearchRawInputToClassSearchInput方法时,它将返回ClassSearchInputViewModel的对象,但它返回的是0而不是item的locationids
这是我对同一个
的单元测试 [Isolated]
[TestMethod]
public void ClassSearchRawInputToClassSearchInputViewModel_Test()
{
var urlProcessor = new SegmentToUrlSeoProcessingResultResolver();
AutoMapperConfiguration.Configure();
IMapperService mapper = new MapperService();
int[] genericStringArray = {1, 2, 3};
var searchRawInput = new SearchRawInput()
{
City = "Mumbai",
Segment1 = "Dance",
Segment2 = "Salsa",
CategoryIds = genericStringArray.ToList(),
StartDate = DateTime.Now,
EndDate = DateTime.Now,
LocationGroupIds = genericStringArray.ToList(),
LocationIds = genericStringArray.ToList(),
StartPrice = genericStringArray[0],
EndPrice = 2132123,
SearchQuery = "Search Query",
SubCategoryIds = genericStringArray.ToList(),
TargetAge = TargetAgeGroup.Kids
};
Isolate.NonPublic.WhenCalled(urlProcessor, "ResolveCore", new SearchRawInput()
{
City = "Mumbai",
Segment1 = "Dance",
Segment2 = "Salsa",
CategoryIds = genericStringArray.ToList(),
StartDate = DateTime.Now,
EndDate = DateTime.Now,
LocationGroupIds = genericStringArray.ToList(),
LocationIds = genericStringArray.ToList(),
StartPrice = genericStringArray[0],
EndPrice = 2132123,
SearchQuery = "Search Query",
SubCategoryIds = genericStringArray.ToList(),
TargetAge = TargetAgeGroup.Kids
}).WillReturn(new UrlSeoProcessingResult()
{
LocationGroupId = 1,
SubCategoryId = 2,
IsError = false,
Type = SegmentType.GroupSubCategory
});
var classSearchInput = mapper.ClassSearchRawInputToClassSearchInput(searchRawInput);
Assert.AreEqual(TargetAgeGroup.Kids, classSearchInput.TargetAge, "Age is not mapped properly");
Assert.AreEqual(3,classSearchInput.LocationIds.Count,"Location not mapped properly");
Assert.AreEqual(3,classSearchInput.CategoryIds.Count,"Category is not mapped correctly");
}
答案 0 :(得分:1)
抱歉,我无法使用最新的AutoMapper NuGet(2.2.1)重现您所描述的问题:
using System;
using System.Collections.Generic;
using AutoMapper;
public class A
{
public int a { get;set; }
public string b { get;set; }
public List<int> ints { get;set; }
}
public class B
{
public int a { get;set; }
public string b { get;set; }
public List<int> ints { get;set; }
}
class Program
{
static void Main()
{
Mapper.CreateMap<B, A>();
var b = new B
{
a = 1,
b = "foo",
ints = new List<int>(new[] { 1, 2, 3, 4 })
};
var a = Mapper.Map<B, A>(b);
Console.WriteLine(a.ints.Count);
foreach (var item in a.ints)
{
Console.WriteLine(item);
}
}
}
此控制台应用程序按预期在两种类型之间进行映射。所以我猜你没有展示你真正的代码。您必须显示一个说明问题的完整示例。