meJustAndrew meJustAndrew - 1 month ago 16
C# Question

An unhandled exception of type 'System.TypeInitializationException' occurred in ConsoleApplication.exe - Automapper

After taking a look at Automapper attributes I have tried to answer this question so I have made a quick Console application to reproduce the behavior. I have added (copy-pasted) the classes in the first example from the GitHub documentation:

[MapsTo(typeof(Customer))]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Notes { get; set; }
}

public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MyCustomerNotes { get; set; }
}


And added the same statements in the main:

typeof(Program).Assembly.MapTypes(); //this throws exception
var person = new Person { FirstName = "John", LastName = "Lackey" };
var customer = AutoMapper.Mapper.Map<Customer>(person);


but at the first line of code in main, when calling
MapTypes
method, an exception of type TypeInitializationException is thrown:


An unhandled exception of type 'System.TypeInitializationException' occurred in ConsoleApplication.exe


Additional information: The type initializer for 'AutoMapper.Attributes.Extensions' threw an exception.

Why is this exception thrown, since I have followed all the instructions in the documentation?

The inner exception is:


{"Sequence contains no matching element"}


NOTE:

I am using AutoMapper version 5.1.1 and AutoMapper.Attributes version 1.0.20.

Answer

Looks like you are using Automapper 5, but the Automapper.Attributes seems only to work with version 4. I tried it with version 4 and it worked as expected.

The problem seems to be that a method signature changed, which is looked up via reflection in Attributes.Extensions

Comments