自动映射器:映射到受保护的属性

编程入门 行业动态 更新时间:2024-10-27 23:29:05
本文介绍了自动映射器:映射到受保护的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用Automapper映射到类的protected属性.我在此类上公开了一个public方法,该方法用于为属性设置值.此方法需要parameter.如何将值映射到此类?

I need to map to a protected property on a class using Automapper. I've got a public method exposed on this class that is used to set values to the property. This method requires a parameter. How can I map a value to this class?

目的地类别:

public class Policy { private Billing _billing; protected Billing Billing { get { return _billing; } set { _billing = value; } } public void SetBilling(Billing billing) { if (billing != null) { Billing = billing; } else { throw new NullReferenceException("Billing can't be null"); } } }

这是我的Automapper代码(伪代码)的样子:

Here's what my Automapper code (pseudo code) looks like:

Mapper.CreateMap<PolicyDetail, Policy>() .ForMember(d => d.SetBilling(???), s => s.MapFrom(x => x.Billing));

我需要将Billing类传递给SetBilling(Billing billing)方法.我该怎么做呢?或者,我可以只设置受保护的结算属性吗?

I need to pass a Billing class to the SetBilling(Billing billing) method. How do I do this? Or, can I just set the protected Billing property?

推荐答案

最简单的方法:使用AfterMap/BeforeMap构造.

Easiest way: Use AfterMap/BeforeMap constructs.

Mapper.CreateMap<PolicyDetail, Policy>() .AfterMap((src, dest) => dest.SetBilling(src.Billing));

github/AutoMapper/AutoMapper/wiki/Before-and -after-map-actions

更多推荐

自动映射器:映射到受保护的属性

本文发布于:2023-11-17 05:39:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   映射器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!