Post

AutoMapper ForAllOtherMembers

ForAllOtherMembers was removed
That was used to disable mapping by convention, not something we want to support.
AutoMapper 11 came with a breaking change, which we all hate. I hope this blog post will end your search.

After digging into Stackoverflow and Internet, I found a dirty way to work with the breaking change.

Create an extension Method called ForAllOtherMembers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 public static class AutomapperExtensions
 {
        private static readonly PropertyInfo TypeMapActionsProperty = typeof(TypeMapConfiguration).GetProperty("TypeMapActions", BindingFlags.NonPublic | BindingFlags.Instance);

        private static readonly PropertyInfo DestinationTypeDetailsProperty = typeof(TypeMap).GetProperty("DestinationTypeDetails", BindingFlags.NonPublic | BindingFlags.Instance);


        public static void ForAllOtherMembers<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression, Action<IMemberConfigurationExpression<TSource, TDestination, object>> memberOptions)
        {
            var typeMapConfiguration = (TypeMapConfiguration)expression;

            var typeMapActions = (List<Action<TypeMap>>)TypeMapActionsProperty.GetValue(typeMapConfiguration);

            typeMapActions.Add(typeMap =>
            {
                var destinationTypeDetails = (TypeDetails)DestinationTypeDetailsProperty.GetValue(typeMap);

                foreach (var accessor in destinationTypeDetails.WriteAccessors.Where(m => typeMapConfiguration.GetDestinationMemberConfiguration(m) == null))
                {
                    expression.ForMember(accessor.Name, memberOptions);
                }
            });
        }
 }

there are more ways which you can find in this stack overflow article.

PS: I would not recommend this, because this method goes against the auto mapper’s design philosophy.
As mentioned in the GitHub discussion, the author of AutoMapper “regrets ever adding it in the first place”.
Cheers
Happy coding :)

This post is licensed under CC BY 4.0 by the author.