title: How to apply AutoMapper ValueConverters against properties that may be null
body: <p>I think this is an AutoMapper bug but the issue template they have in GitHub states to post something to SO first.</p>
  <p>I want to be able to apply an <code>IValueConverter</code> without worrying about null exceptions.</p>
  <p>As an example, I'm using a <code>IValueConverter</code> to apply some logic in multiple mappings:</p>
  <pre class="lang-cs prettyprint-override"><code>public class ExampleConverter : IValueConverter&lt;string, string&gt;
  {
      public string Convert(string sourceMember, ResolutionContext context)
      {
          if (string.IsNullOrEmpty(sourceMember))
          {
              return string.Empty;
          }
  
          return sourceMember.ToUpper();
      }
  }
  </code></pre>
  <p>If I have the following types I'm mapping to and from:</p>
  <pre class="lang-cs prettyprint-override"><code>public class ExampleSource
  {
      public ExampleNestedSource1 A { get; set; }
  }
  
  public class ExampleNestedSource1
  {
      public ExampleNestedSource2 B { get; set; }
  }
  
  public class ExampleNestedSource2
  {
      public string Input { get; set; }
  }
  
  public class ExampleDestination
  {
      public string Output { get; set; }
  }
  </code></pre>
  <p>I can apply the converter like so:</p>
  <pre class="lang-cs prettyprint-override"><code>public class ExampleProfile : Profile
  {
      public ExampleProfile()
      {
          this.CreateMap&lt;ExampleSource, ExampleDestination&gt;(MemberList.None)
              .ForMember(dst =&gt; dst.Output, opt =&gt; opt.ConvertUsing(new ExampleConverter(), src =&gt; src.A.B.Input));
      }
  }
  </code></pre>
  <p>Using the converter like this however, throws an exception when A or B are null:</p>
  <pre class="lang-cs prettyprint-override"><code>mapper.Map&lt;ExampleDestination&gt;(new ExampleSource // Throws null reference
  {
      A = new ExampleNestedSource1(),
  });
  mapper.Map&lt;ExampleDestination&gt;(new ExampleSource()); // Throws null reference
  </code></pre>
  <p>Because <code>opt.ConvertUsing(new ExampleConverter(), src =&gt; src.A.B.Input)</code> takes in an expression I can't use null propagating operators like <code>src?.A?.B?.Input</code>.</p>
  <p>If I remove the converter and use <code>MapFrom</code>, the problem goes away:</p>
  <pre class="lang-cs prettyprint-override"><code>public class ExampleProfile : Profile
  {
      public ExampleProfile()
      {
          this.CreateMap&lt;ExampleSource, ExampleDestination&gt;(MemberList.None)
              .ForMember(dst =&gt; dst.Output, opt =&gt; opt.MapFrom(src =&gt; src.A.B.Input));
      }
  }
  </code></pre>
  <p>But <code>MapFrom</code> doesn't support <code>IValueConverter</code> it only supports <code>IMemberValueResolver</code> which is less reusable.</p>
  <p>Is there a better way to handle this scenario?</p>
  <p>If not, I'm thinking AutoMapper should either:</p>
  <ul>
  <li>Add support for <code>IValueConverter</code> in the <code>MapFrom</code> method.</li>
  <li><code>ConvertUsing</code> should not invoke the given converter when the expression doesn't resolve to a property and not throw.</li>
  <li><code>ConvertUsing</code> should invoke the given converter with null when the expression doesn't resolve to a property (in the same way that <code>MapFrom</code>) and not throw.</li>
  </ul>