Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3334860
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:57:06+00:00 2026-05-17T23:57:06+00:00

I’m using AutoMapper and I’d like it to trace back a source property based

  • 0

I’m using AutoMapper and I’d like it to trace back a source property based on the name of the mapped (flattened) destination property.

This is because my MVC controller has the name of a mapped property that it needs to provide to a service call that for sorting purposes. The service needs to know the name of the property that the mapping originated from (and the controller is not supposed to know it) in order to perform a proper call to the repository that actually sorts the data.

For example:

[Source.Address.ZipCode] maps to [Destination.AddressZipCode]

Then

Trace “AddressZipCode” back to [Source.Address.ZipCode]

Is this something that AutoMapper can do for me or do I need to resort to digging into AutoMapper’s mapping data?

UPDATE

Jimmy Bogard told me that this should be possible but not in an obvious manner. It requires loading the type map and going through it. I’ve looked into it briefly but it seems that I need access to internal types to get to the property mapping information that is required to do reverse mapping.

UPDATE 2

I’ve decided to provide some more details.

When I load up the type map, I find that there are two source value resolvers in it for the implicit ZipCode mapping:

  • a AutoMapper.Internal.PropertyGetter that gets the Address.
  • a AutoMapper.Internal.PropertyGetter that gets the ZipCode.

When I have an explicit mapping (that has a lambda expression specified), I find no source value resolver but a custom resolver:

  • a AutoMapper.DelegateBasedResolver<Company,string> that I think holds my explicit mapping lambda expression.

Unfortunately these resolvers are internal so I can only access them through reflection (which I really don’t want to do) or by changing the AutoMapper source code.

If I could access them, I could solve the problem by either walking through the value resolvers or by inspecting the custom resolver although I doubt it that would lead me back to the mapping lambda expression which I need to build the unflattened property name (actually a series of property names separated by dots).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:57:07+00:00Added an answer on May 17, 2026 at 11:57 pm

    For the time being, I wrote a helper class that can determine the originating property chain from a concatenated property chain. Ofcourse, this will become obsolete when AutoMapper gets a feature to do this kind of thing.

    using System.Globalization;
    using System.Reflection;
    
    /// <summary>
    ///     Resolves concatenated property names back to their originating properties.
    /// </summary>
    /// <remarks>
    ///     An example of a concatenated property name is "ProductNameLength" where the originating
    ///     property would be "Product.Name.Length".
    /// </remarks>
    public static class ConcatenatedPropertyNameResolver
    {
        private static readonly object mappingCacheLock = new object();
        private static readonly Dictionary<MappingCacheKey, string> mappingCache = new Dictionary<MappingCacheKey, string>();
    
        /// <summary>
        ///     Returns the nested name of the property the specified concatenated property
        ///     originates from.
        /// </summary>
        /// <param name="concatenatedPropertyName">The concatenated property name.</param>
        /// <typeparam name="TSource">The mapping source type.</typeparam>
        /// <typeparam name="TDestination">The mapping destination type.</typeparam>
        /// <returns>
        ///     The nested name of the originating property where each level is separated by a dot.
        /// </returns>
        public static string GetOriginatingPropertyName<TSource, TDestination>(string concatenatedPropertyName)
        {
            if (concatenatedPropertyName == null)
            {
                throw new ArgumentNullException("concatenatedPropertyName");
            }
            else if (concatenatedPropertyName.Length == 0)
            {
                throw new ArgumentException("Cannot be empty.", "concatenatedPropertyName");
            }
    
            lock (mappingCacheLock)
            {
                MappingCacheKey key = new MappingCacheKey(typeof(TSource), typeof(TDestination), concatenatedPropertyName);
    
                if (!mappingCache.ContainsKey(key))
                {
                    BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;
    
                    List<string> result = new List<string>();
                    Type type = typeof(TSource);
    
                    while (concatenatedPropertyName.Length > 0)
                    {
                        IEnumerable<PropertyInfo> properties = type.GetProperties(bindingFlags).Where(
                            n => concatenatedPropertyName.StartsWith(n.Name)).ToList();
    
                        if (properties.Count() == 1)
                        {
                            string match = properties.First().Name;
                            result.Add(match);
                            concatenatedPropertyName = concatenatedPropertyName.Substring(match.Length);
                            type = type.GetProperty(match, bindingFlags).PropertyType;
                        }
                        else if (properties.Any())
                        {
                            throw new InvalidOperationException(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    "Ambiguous properties found for {0} on type {1}: {2}.",
                                    concatenatedPropertyName,
                                    typeof(TSource).FullName,
                                    string.Join(", ", properties.Select(n => n.Name))));
                        }
                        else
                        {
                            throw new InvalidOperationException(
                                string.Format(
                                    CultureInfo.InvariantCulture,
                                    "No matching property found for {0} on type {1}.",
                                    concatenatedPropertyName,
                                    typeof(TSource).FullName));
                        }
                    }
    
                    mappingCache.Add(key, string.Join(".", result));
                }
    
                return mappingCache[key];
            }
        }
    
        /// <summary>
        ///     A mapping cache key.
        /// </summary>
        private struct MappingCacheKey
        {
            /// <summary>
            ///     The source type.
            /// </summary>
            public Type SourceType;
    
            /// <summary>
            ///     The destination type the source type maps to. 
            /// </summary>
            public Type DestinationType;
    
            /// <summary>
            ///     The name of the mapped property.
            /// </summary>
            public string MappedPropertyName;
    
            /// <summary>
            ///     Initializes a new instance of the <see cref="MappingCacheKey"/> class.
            /// </summary>
            /// <param name="sourceType">The source type.</param>
            /// <param name="destinationType">The destination type the source type maps to.</param>
            /// <param name="mappedPropertyName">The name of the mapped property.</param>
            public MappingCacheKey(Type sourceType, Type destinationType, string mappedPropertyName)
            {
                SourceType = sourceType;
                DestinationType = destinationType;
                MappedPropertyName = mappedPropertyName;
            }
        }
    }
    

    Here’s a usage example:

    class TestEntity
    {
        public Node Root {get; set;}
    }
    
    class Node
    {
        public string Leaf {get; set;}
    }
    
    class TestFlattenedEntity
    {
        public string RootLeaf {get; set;}
    }
    
    string result = ConcatenatedPropertyNameResolver.GetOriginatingPropertyName<TestEntity, TestFlattenedEntity>("RootLeaf");
    
    Assert.AreEqual("Root.Leaf", result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.