none
[Automapper]How to get source path of mapping by destination path. RRS feed

  • Question

  • Hi,

    For generic filtering of external system i need to get source path of mapping by destination path.


                    CreateMap<Record, Contact>()
                    .ForMember(x => x.Id, opt => opt.MapFrom(c => c.Id))
                    .ForMember(x => x.PersonInformation, opt => opt.MapFrom(c=>
                        new PersonInformation()
                        {                        
                            FirstName = c.FirstName,
                            LastName = c.LastName,
                            MiddleName = c.MiddleName,
                            ExternalReferences = new List<ExternalReference>() { new ExternalReference { Id = 1, Ref = c.Id } }
                        }
                    ))

    I created function but it works only for simple paths like:
    Contact.Id => Record.Id
    but it dont work with
    Contact.PersonInformation.FirstName =will be mapped to> Record.FirstName

            public (PropertyInfo propertyInfo, string path) GetDestinationPropertyFor(string sourcePropertyPath, Type salesforceType, Type swaggerType)
            {
                var properties = sourcePropertyPath.Split('.');
                var destinationProperties = new List<string>();
                PropertyInfo propertyInfo = null;


                foreach (var property in properties)
                {
                    var map = _mapper.FindTypeMapFor(salesforceType, swaggerType);
                    var propertyMap = map.PropertyMaps.First(pm => pm.DestinationMember == swaggerType.GetProperty(property));


                    propertyInfo = propertyMap.SourceMember as PropertyInfo;


                    var jsonProperty = propertyInfo.GetCustomAttribute(typeof(JsonPropertyAttribute));
                    destinationProperties.Add(((JsonPropertyAttribute)jsonProperty).PropertyName);


                    swaggerType = propertyMap.Types.DestinationType;
                    salesforceType = propertyMap.Types.SourceType;
                }


                var destinationPropertyPath = string.Join('.', destinationProperties);


                return (propertyInfo, destinationPropertyPath);
            }
    • Moved by CoolDadTx Wednesday, April 10, 2019 1:52 PM Third party product
    Wednesday, April 10, 2019 8:56 AM

All replies

  • Automapper is a a group you can post to. You may also want to check StackOverflow too.

    https://groups.google.com/forum/#!forum/automapper-users

    MSDN does not support 3rd party software.

    Wednesday, April 10, 2019 11:28 AM