title: AutoMapper: How to parse an Int from a String and possible to creating rules based on data type?
body: <p>I have two models for my form, a ViewModel going to it, and a ControlModel coming from it.  The ControlModel has all the same field names and hierarchy, but all of the fields are a string data type.</p>
  
  <p><strong>How would you code AutoMapper to convert a string field to integer?</strong>  I tried Int32.Parse(myString) but Int32 is not available within the expression (gives an error).</p>
  
  <pre><code>Mapper.CreateMap&lt;SourceClass, DestinationClass&gt;()
        .ForMember(dest =&gt; dest.myInteger, 
                    opt =&gt; opt.MapFrom(src =&gt; src.myString));
  </code></pre>
  
  <p>The types in the class and their corresponding conversion types:</p>
  
  <p>string to int, int?, double, double?, DateTime, and bool</p>
  
  <p>Additionally, is there any way to generalize mappings in a way that all integers in the target are parsed with that function?  In other words, is there a way to create mappings for data types?</p>
  
  <p>EDIT:</p>
  
  <p>This looks promising:</p>
  
  <pre><code>AutoMapper.Mapper.CreateMap&lt;string, int&gt;()
            .ConvertUsing(src =&gt; Convert.ToInt32(src));
  </code></pre>
  
  <p>EDIT:
  This <a href="https://stackoverflow.com/questions/2560741/automapper-type-converter-from-string-to-ienumerablestring-is-not-being-called">post</a> is really helpful</p>