AutoMapper通用映射 [英] AutoMapper generic mapping

查看:95
本文介绍了AutoMapper通用映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在StackOverflow上进行了搜索,并在Google上进行了搜索,但是我找不到任何帮助或建议.

I have searched on StackOverflow and googled about it but I havent been able to find any help or suggestion on this.

我有一个类似于以下的类,其中创建了一个PagedList对象,并且还使用AutoMappper将类型从源映射到目标

I have a class like the following wich create a PagedList object and also uses AutoMappper to map types from source to destination

public class PagedList<TSrc, TDest>
{
    protected readonly List<TDest> _items = new List<TDest>();

    public IEnumerable<TDest> Items {
        get { return this._items; }
    }
}

我想为此类型创建一个地图,该地图应将其转换为如下所示的另一种类型

I would like to create a Map for this type that should convert it to another type like the following

public class PagedListViewModel<TDest>
{
    public IEnumerable<TDest> Items { get; set; }
}

我尝试过

Mapper.CreateMap<PagedList<TSrc, TDest>, PagedListViewModel<TDest>>();

,但是编译器抱怨是由于TSrcTDest

but the compiler complains because of TSrc and TDest

有什么建议吗?

推荐答案

根据 AutoMapper Wiki :

public class Source<T> {
    public T Value { get; set; }
}

public class Destination<T> {
    public T Value { get; set; }
}

// Create the mapping
Mapper.CreateMap(typeof(Source<>), typeof(Destination<>));

您的情况应该是

Mapper.CreateMap(typeof(PagedList<,>), typeof(PagedListViewModel<>));

这篇关于AutoMapper通用映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆