简体   繁体   English

具有多个ITypeConverter实现的automapper ConvertUsing

[英]automapper ConvertUsing with multiple ITypeConverter implementations

I have a typeconverter class that implements ITypeConverter for both an object and for a pager (inheriting IEnumerable) of the objects. 我有一个typeconverter类,它为对象和对象的寻呼机(继承IEnumerable)都实现了ITypeConverter。 eg 例如

   class MyConverter : ITypeConverter<IFoo, FooModel>,
                       ITypeConverter<IPager<IFoo>, IPager<FooModel>>
    {
        public FooModel Convert(IFoo source, FooModel destination, ...) {...}
        public IPager<FooModel> Convert(IPager<IFoo> source, IPager<FooModel> destination, ...) {...}

    }

When given a pager, it uses the single object converter implementation anyway. 给定一个寻呼机后,它仍然使用单个对象转换器实现。

I know that Automapper will map collections automatically but shouldn't it prefer an explicit collection mapping if one exists? 我知道Automapper会自动映射集合,但是如果存在的话,它不应该选择显式集合映射吗? I could probably move the implementations into separate classes but is there another way to get it to use the pager implementation? 我可能可以将实现移动到单独的类中,但是还有另一种方法可以使它使用寻呼机实现吗?

You should have called CreateMap and ConvertUsing - did you do this? 您应该已经调用CreateMap和ConvertUsing-您这样做吗?

cfg.CreateMap<IFoo, FooModel>().ConvertUsing<MyConverter>();
cfg.CreateMap<IPager<IFoo>, IPager<FooModel>>().ConvertUsing<MyConverter>();

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM