我正在使用 AutoMapper 试图简化来自某些 REST API 响应的响应。我有一个带有名为 Messages 的数组属性的对象,我想将它映射到我的客户端应用程序中的列表<> 或数组。
我尝试了以下变化:
Mapper.CreateMap<MessagesResponse, Message[]>()
.ForMember(dest => dest, opt => opt.MapFrom(src => src.Messages));
....
Mapper.CreateMap<MessagesResponse, List<SimplifiedMessage>>().ForMember(dest => dest.Add(new SimplifiedMessage()), opt => opt.MapFrom(src => src.Messages.GetEnumerator()));
...但显然这些似乎都不起作用。理想情况下,我希望得到一个 SimplifiedMessage 列表,并且我很确定我错过了迭代 Messages[] 的正确语法。
public class MessagesResponse
{
[JsonProperty("meta")]
public Meta Meta { get; set; }
[JsonProperty("references")]
public Reference[] References { get; set; }
[JsonProperty("messages")]
public Message[] Messages { get; set; }
[JsonProperty("threaded_extended")]
public ThreadedExtended ThreadedExtended { get; set; }
}
public class Message
{
[JsonProperty("content_excerpt")]
public string ContentExcerpt { get; set; }
[JsonProperty("chat_client_sequence")]
public object ChatClientSequence { get; set; }
[JsonProperty("replied_to_id")]
public int? RepliedToId { get; set; }
[JsonProperty("client_url")]
public string ClientUrl { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("sender_type")]
public string SenderType { get; set; }
[JsonProperty("attachments")]
public Attachment[] Attachments { get; set; }
[JsonProperty("direct_message")]
public bool DirectMessage { get; set; }
[JsonProperty("privacy")]
public string Privacy { get; set; }
[JsonProperty("body")]
public Body2 Body { get; set; }
[JsonProperty("sender_id")]
public int SenderId { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("thread_id")]
public int ThreadId { get; set; }
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("notified_user_ids")]
public int[] NotifiedUserIds { get; set; }
[JsonProperty("created_at")]
public string CreatedAt { get; set; }
[JsonProperty("message_type")]
public string MessageType { get; set; }
[JsonProperty("system_message")]
public bool SystemMessage { get; set; }
[JsonProperty("client_type")]
public string ClientType { get; set; }
[JsonProperty("web_url")]
public string WebUrl { get; set; }
[JsonProperty("group_id")]
public int GroupId { get; set; }
[JsonProperty("network_id")]
public int NetworkId { get; set; }
}