Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically mapping with Automapper
    primarykey
    data
    text
    <p>I'm trying to conditionally map two objects based on <code>ExtendedField.type</code>. So if the type is <code>textbox</code> then I would map to the <code>TextBox</code> class, but if it's <code>checkbox</code> then I would map to <code>Checkbox</code> class. And of course this needs to be open for extension to map to other IHtmlElement derived types.</p> <pre><code>Mapper.Map&lt;IEnumerable&lt;ExtendedField, IEnumerable&lt;IHtmlElement&gt;&gt;(extendedFields); </code></pre> <p>A sample of the objects:</p> <pre><code>public class ExtendedField { public string type { get; set; } public string prompt { get; set; } public string value { get; set; } } public Interface IHtmlElement { string label { get; set; } string type { get; set; } string value { get; set; } } public class TextBox : IHtmlElement { public string label { get; set; } public string type { get { return "textbox"; } } public string value { get; set; } } public class CheckBox : IHtmlElement { public string label { get; set; } public string type { get { return "checkbox"; } } public string value { get; set; } } </code></pre> <p>I have created the mapping to map to <code>IHtmlElement</code> but I can't think of how to dynamically tell AutoMapper which concrete class to map to based off the <code>type</code> property.</p> <pre><code>Mapper.CreateMap&lt;ExtendedField, IHtmlElement&gt;() .ForMember(dest =&gt; dest.label, opt =&gt; opt.MapFrom(src =&gt; src.prompt)) .ForMember(dest =&gt; dest.type, opt =&gt; opt.MapFrom(src =&gt; src.type)) .ForMember(dest =&gt; dest.value, opt =&gt; opt.MapFrom(src =&gt; src.extendedFieldValue)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload