Automapper未能地图上的IEnumerable [英] Automapper failing to map on IEnumerable
问题描述
我有两个类,像这样:
公共类SentEmailAttachment:ISentEmailAttachment
{
公共SentEmailAttachment ();
公共字符串文件名{获得;组; }
公共字符串ID {搞定;组; }
公共字符串SentEmailID {搞定;组; }
公共字符串StorageService {搞定;组; }
公共字符串StorageServiceFileID {搞定;组; }
}
和
公共类SentEmailAttachmentItem:ISentEmailAttachment
{
[ITEMNAME]
公共字符串ID {搞定;组; }
公共字符串SentEmailID {搞定;组; }
公共字符串文件名{获得;组; }
公共字符串StorageService {搞定;组; }
公共字符串StorageServiceFileID {搞定;组; }
}
相同,你可以看到(它们都实现接口,以确保本)
然后我有以下映射:
Mapper.CreateMap< IEnumerable的< SentEmailAttachmentItem>中的IEnumerable< SentEmailAttachment>>();
Mapper.CreateMap<&IEnumerable的LT; SentEmailAttachment>中的IEnumerable< SentEmailAttachmentItem>>();
然后我有以下的单元测试:
//创建发送电子邮件附件
变种listOfSentEmailAttachments =新名单℃的负荷; SentEmailAttachment>();
的for(int i = 0;我小于10;我++)
listOfSentEmailAttachments.Add(新SentEmailAttachment {文件名=testFileName,ID = Guid.NewGuid()的ToString() ,SentEmailID = Guid.NewGuid()的ToString(),StorageService =S3,StorageServiceFileID =SomeFileID});
VAR sentEmailAttachmentItems = Mapper.DynamicMap<&IEnumerable的LT; SentEmailAttachment>中的IEnumerable< SentEmailAttachmentItem>>(listOfSentEmailAttachments);
VAR itemToTest = sentEmailAttachmentItems.First();
Assert.IsInstanceOfType(itemToTest的typeof(SentEmailAttachmentItem));
这失败 -
了IEnumerable sentEmailAttachmentItems是空的。
它没有映射SentEmailAttachments吧...
任何想法是怎么回事?
$ B名单$ b
我有它的工作单的对象(映射每到每个之一中的一个),但不是一个集...
您无需显式地映射集合类型,只有项目类型。只要做到:
Mapper.CreateMap< SentEmailAttachment,SentEmailAttachmentItem>();
VAR附件= Mapper.Map<&IEnumerable的LT; SentEmailAttachment>中列出< SentEmailAttachmentItem>>(someList);
这会工作得很好。
I have two classes like so:
public class SentEmailAttachment : ISentEmailAttachment
{
public SentEmailAttachment();
public string FileName { get; set; }
public string ID { get; set; }
public string SentEmailID { get; set; }
public string StorageService { get; set; }
public string StorageServiceFileID { get; set; }
}
And
public class SentEmailAttachmentItem : ISentEmailAttachment
{
[ItemName]
public string ID { get; set; }
public string SentEmailID { get; set; }
public string FileName { get; set; }
public string StorageService { get; set; }
public string StorageServiceFileID { get; set; }
}
Identical, as you can see (they both implement interface to ensure this)
I then have the following mapping:
Mapper.CreateMap<IEnumerable<SentEmailAttachmentItem>, IEnumerable<SentEmailAttachment>>();
Mapper.CreateMap<IEnumerable<SentEmailAttachment>, IEnumerable<SentEmailAttachmentItem>>();
I then have the following Unit test:
//create a load of sent email attachments
var listOfSentEmailAttachments = new List<SentEmailAttachment>();
for (int i = 0; i < 10; i++)
listOfSentEmailAttachments.Add(new SentEmailAttachment { FileName = "testFileName", ID = Guid.NewGuid().ToString(), SentEmailID = Guid.NewGuid().ToString(), StorageService = "S3", StorageServiceFileID = "SomeFileID" });
var sentEmailAttachmentItems = Mapper.DynamicMap<IEnumerable<SentEmailAttachment>, IEnumerable<SentEmailAttachmentItem>>(listOfSentEmailAttachments);
var itemToTest = sentEmailAttachmentItems.First();
Assert.IsInstanceOfType(itemToTest, typeof(SentEmailAttachmentItem));
This fails - The IEnumerable sentEmailAttachmentItems is empty. It didn't map the list of SentEmailAttachments to it...
Any idea what's going on??
I have it working on single objects (mapping one of each to one of each) but not a collection...
You do not need to explicitly map collection types, only the item types. Just do:
Mapper.CreateMap<SentEmailAttachment, SentEmailAttachmentItem>();
var attachments = Mapper.Map<IEnumerable<SentEmailAttachment>, List<SentEmailAttachmentItem>>(someList);
That will work just fine.
这篇关于Automapper未能地图上的IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!