Search code examples
c#automapper-3

AutoMapperMappingException Get Property name


I've got a custom adapter pattern like so

public interface IFoo
{
     byte[] Content{get; set;}
}

public class FooDTO : IFoo
{
    byte[] Content {get; set;}
}

public FooAdapter : IFoo
{
    Picture _picture;
    public FooAdapter(Picture picture)
    {
        this._picture = picture;
    }
   
    public byte[] Content => this._picture.GetContent();
}

Then I Have an Extension method to convert my entities to a dto like so

public static FooDTO ToDTO(this IFoo getter)
{
    return AutoMapperExtensions.Map<IFoo, FooDTO >(getter);            
}

This adapter adapters between the same interface IFoo, Occasionally I Get AutoMapperMappingExceptions

Mapping types: FooAdapter -> Byte[] MyNamespace.FooAdapter -> System.Byte[]

Destination path: FooDTO.Content

Source value: MyNamespace.FooAdapter

All that information is nice but how can I get the property name of the property that has thrown the error?


Solution

  • Incase anyone was wondering you can access the Member name through the error context.

    catch(AutoMapperMappingException mappingException)
    {
        BuildNewError(mappingException.Context.MemberName);
    }