snipt

Ctrl+h for KB shortcuts

C#

AutoMapper: Ignoring mapping for a defined set of classes

1
2
3
4
5
6
7
8
// Globally defined ignore for mapping of spesific properties
// In this case: IRinaDokumentModel.Vedlegg and IRinaDokumentModel.OriginalRinaDokument
// for all classes that implement interface IRinaDokumentModel

(Profile)this.ShouldMapProperty = p =>
    !(p.DeclaringType.GetInterface(nameof(IRinaDokumentModel)) != null
      && (p.Name == nameof(IRinaDokumentModel.Vedlegg)
          || p.Name == nameof(IRinaDokumentModel.OriginalRinaDokument)));

Description

Globally defined ignore for mapping of spesific properties
In this case: IRinaDokumentModel.Vedlegg and IRinaDokumentModel.OriginalRinaDokument
for all classes that implement interface IRinaDokumentModel
https://snipt.net/embed/a03e56370043cd04d0b12b7e3d3395a3/
/raw/a03e56370043cd04d0b12b7e3d3395a3/
a03e56370043cd04d0b12b7e3d3395a3
csharp
C#
8
2019-05-21T19:02:31
True
False
False
/api/public/snipt/148349/
fec7e03b
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// Globally defined ignore for mapping of spesific properties</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// In this case: IRinaDokumentModel.Vedlegg and IRinaDokumentModel.OriginalRinaDokument</span> </span><span id="L-3"><a name="L-3"></a><span class="c1">// for all classes that implement interface IRinaDokumentModel</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="p">(</span><span class="n">Profile</span><span class="p">)</span><span class="k">this</span><span class="p">.</span><span class="n">ShouldMapProperty</span> <span class="p">=</span> <span class="n">p</span> <span class="p">=&gt;</span> </span><span id="L-6"><a name="L-6"></a> <span class="p">!(</span><span class="n">p</span><span class="p">.</span><span class="n">DeclaringType</span><span class="p">.</span><span class="n">GetInterface</span><span class="p">(</span><span class="n">nameof</span><span class="p">(</span><span class="n">IRinaDokumentModel</span><span class="p">))</span> <span class="p">!=</span> <span class="k">null</span> </span><span id="L-7"><a name="L-7"></a> <span class="p">&amp;&amp;</span> <span class="p">(</span><span class="n">p</span><span class="p">.</span><span class="n">Name</span> <span class="p">==</span> <span class="n">nameof</span><span class="p">(</span><span class="n">IRinaDokumentModel</span><span class="p">.</span><span class="n">Vedlegg</span><span class="p">)</span> </span><span id="L-8"><a name="L-8"></a> <span class="p">||</span> <span class="n">p</span><span class="p">.</span><span class="n">Name</span> <span class="p">==</span> <span class="n">nameof</span><span class="p">(</span><span class="n">IRinaDokumentModel</span><span class="p">.</span><span class="n">OriginalRinaDokument</span><span class="p">)));</span> </span></pre></div> </td></tr></table>
automapper
--- 
+++ 
@@ -0,0 +1,8 @@
+// Globally defined ignore for mapping of spesific properties
+// In this case: IRinaDokumentModel.Vedlegg and IRinaDokumentModel.OriginalRinaDokument
+// for all classes that implement interface IRinaDokumentModel
+
+(Profile)this.ShouldMapProperty = p =>
+    !(p.DeclaringType.GetInterface(nameof(IRinaDokumentModel)) != null
+      && (p.Name == nameof(IRinaDokumentModel.Vedlegg)
+          || p.Name == nameof(IRinaDokumentModel.OriginalRinaDokument)));