自动映射器:映射到受保护的属性 [英] Automapper: Map to Protected Property

查看:88
本文介绍了自动映射器:映射到受保护的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用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));

https://github.com/AutoMapper/AutoMapper/wiki/Before-and -after-map-actions

这篇关于自动映射器:映射到受保护的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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