Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've managed to do what I wanted. Here is the main class with an extension method to add an ignored property to an existing map:</p> <pre class="lang-cs prettyprint-override"><code> static class TypeMapExtensions { public static TypeMap&lt;TSource, TDestination&gt; AddIgnoredMember&lt;TSource, TDestination&gt;( this TypeMap&lt;TSource, TDestination&gt; typeMap, Expression&lt;Func&lt;TDestination, object&gt;&gt; accessorExpression) { MemberInfo memberInfo = ReflectionHelper.FindProperty(accessorExpression); IMemberAccessor memberAccessor = memberInfo.ToMemberAccessor(); var propertyMap = new PropertyMap(memberAccessor); propertyMap.Ignore(); AddPropertyMap(typeMap.Map, propertyMap); return typeMap; } private static void AddPropertyMap(TypeMap typeMap, PropertyMap propertyMap) { typeMap.AddPropertyMap(propertyMap); if (typeMap.HasDerivedTypesToInclude()) { IEnumerable&lt;TypeMap&gt; derivedMaps = GetDerivedMaps(typeMap); foreach (var derivedMap in derivedMaps) derivedMap.AddInheritedPropertyMap(propertyMap); } } private static IEnumerable&lt;TypeMap&gt; GetDerivedMaps(TypeMap typeMap) { return Mapper.GetAllTypeMaps().Where( map =&gt; (map.Profile == typeMap.Profile) &amp;&amp; (typeMap.TypeHasBeenIncluded(map.SourceType, map.DestinationType))); } } </code></pre> <p>The class TypeMap is nothing more than a wrapper against the original TypeMap. The point of the generic version is to allow fluent syntax (see below).</p> <p>ReflectionHelper class borrowed from the AutoMapper source code. It depends on five other classes from the AutoMapper.Internal namespace, so they were borrowed as well.</p> <p>At the end, all this allows me to write the following code:</p> <pre class="lang-cs prettyprint-override"><code>TypeMap&lt;SourceType, DestinationType&gt;.Get() .AddIgnoredMember(dest =&gt; dest.IgnoredMember) .AddIgnoredMember(dest =&gt; dest.AnotherIgnoredMember); </code></pre>
 

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