c# – 防止Automapper将IEnumerable属性转换为List

c# – 防止Automapper将IEnumerable属性转换为List,第1张

概述我正在尝试将Model映射到ViewModel,该ViewModel具有从IEnumerable继承的类型属性.属性具有相同的类型和名称,但Automapper将源转换为通用列表,然后无法映射到目标. 这些是我想要映射的类: BasicOverview{ public IRichTextContent Intro { get; set; } ...}BlogOverviewVi 我正在尝试将Model映射到viewmodel,该viewmodel具有从IEnumerable继承的类型属性.属性具有相同的类型和名称,但automapper将源转换为通用列表,然后无法映射到目标.

这些是我想要映射的类:

BasicOvervIEw{   public IRichTextContent Intro { get; set; }   ...}BlogovervIEwviewmodel{   public IRichTextContent Intro { get; set; }   ...}

以下是定义IRichTextContent类型的第三方代码:

//     Represents rich text content in a form of structured datapublic interface IRichTextContent : IEnumerable<IRichTextBlock>,IEnumerable{    //    // Summary:    //     List of rich text content blocks    IEnumerable<IRichTextBlock> Blocks { get; set; }}

我的automapper配置文件:

public automapperProfile()    {        CreateMap<BasicOvervIEw,BlogListviewmodel>();        CreateMap<BasicOvervIEw,RevIEwListviewmodel>();        CreateMap<BasicOvervIEw,BlogovervIEwviewmodel>();    }

这是我得到的错误:

An unhandled exception occurred while processing the request.
InvalIDCastException: Unable to cast object of type ‘System.Collections.Generic.List`1[KenticoCloud.Delivery.IRichTextBlock]’ to type ‘KenticoCloud.Delivery.IRichTextContent’.
lambda_method(Closure,BasicOvervIEw,BlogovervIEwviewmodel,ResolutionContext )

autoMapperMapPingException: Error mapPing types.

MapPing types:
BasicOvervIEw -> BlogovervIEwviewmodel

Type Map configuration:
BasicOvervIEw -> BlogovervIEwviewmodel

Destination Member:
Intro
lambda_method(Closure,ResolutionContext )

我尝试将以下内容添加到我的automapper配置文件中:

CreateMap<IEnumerable<IRichTextBlock>,IRichTextContent>()            .ForMember(dest => dest.Blocks,m => m.MapFrom(src => src));

其中产生了以下错误:

TypeLoadException: Method ‘GetEnumerator’ in type ‘Proxy_KenticoCloud.Delivery.IRichTextContent_12345678_’ from assembly ‘autoMapper.ProxIEs,Version=0.0.0.0,Culture=neutral,PublicKeyToken=abc123ef45’ does not have an implementation.

解决方法 只是为了让 Lucian Bargaoanu的评论更加明显.

其中一个解决方案是将以下映射添加到automapper配置文件:

CreateMap<IRichTextContent,IRichTextContent>().ConvertUsing(s=>s);
总结

以上是内存溢出为你收集整理的c# – 防止Automapper将IEnumerable属性转换为List全部内容,希望文章能够帮你解决c# – 防止Automapper将IEnumerable属性转换为List所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1233344.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存